QTimer :: singleShot()在给定对象的父类中查找指定的槽,而不是对象本身 [英] QTimer::singleShot() looks for the specified slot in the given object's parent class, not the object itself

查看:2497
本文介绍了QTimer :: singleShot()在给定对象的父类中查找指定的槽,而不是对象本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Qt相当新。我已经对现有的Qt应用程序做了一些简单的修改,但我还没有从头开始创建任何内容。

我对C ++的某些方面(类继承等)也没有太多经验)。

I am fairly new to Qt. I have done some simple modifications to an existing Qt application, but I haven't created any from scratch yet.
I also don't have really much experience with certain aspects of C++ in general (class inheritance etc).

我创建了一个新的Code :: Blocks基于Qt4的项目并稍微修改了模板。我添加了两个文件。

现在项目包含三个文件:main.cpp,app.h和app.cpp。

这是 main的内容。 cpp

I have created a new Code::Blocks Qt4-based project and modified the template a bit. I have added two files.
Right now the project contains three files: main.cpp, app.h and app.cpp.
This is the content of main.cpp:

#include <QTimer>

#include "app.h"

int main(int argc, char* argv[]) {
    TestApp app(argc, argv);

    QTimer::singleShot(1000, &app, SLOT(timeout()));

    return app.exec();
}

这是 app.h 的样子:

#ifndef APP_H_INCLUDED
#define APP_H_INCLUDED

#include <QApplication>

class TestApp: public QApplication {
    public:
    TestApp(int &argc, char **argv);
    public slots:
    void timeout();
};

#endif

这是 app.cpp

#include "app.h"

#include <QDebug>

TestApp::TestApp(int &argc, char **argv): QApplication(argc, argv) {
}

void TestApp::timeout() {
    qDebug() << "timeout called";
}

我希望程序在启动后一秒打印超时调用。不幸的是,这不起作用。当调用 QTimer :: singleShot()时,控制台会说:

I expected the program to print "timeout called" one second after startup. Unfortunately, this doesn't work. When QTimer::singleShot() is called, the console says:

Object::connect: No such slot QApplication::timeout() in [path to the main.cpp file]
Object::connect:  (receiver name: 'QtTests')

我不知道如何处理这个问题。提前谢谢你。

I have no idea how to deal with this. Thank you in advance.

推荐答案

你只是错过了 Q_OBJECT TestApp类中的宏:

You're simply missing the Q_OBJECT macro in your TestApp class:

class TestApp: public QApplication {
    Q_OBJECT

    public:
    ...

这是整个信号/插槽基础设施工作所必需的(并且从具有此宏的类派生是不够的。)

This is necessary for the whole signal/slot infrastructure to work (and deriving from a class that has this macro is not sufficient).

(确保在更改后执行完整,干净的构建 - 如果您不使用 qmake 或其他一些支持Qt的构建系统,您需要自己运行 moc 。)

(Make sure you do a full, clean build after that change - and if you don't use qmake or some other Qt-aware build system, you'll need to run moc yourself.)

如需参考,请参阅 QObject docs:

For reference, see the QObject docs:


请注意,Q_OBJECT宏对于任何实现信号的对象都是必需的,插槽或属性。您还需要在源文件上运行元对象编译器。我们强烈建议在QObject的所有子类中使用此宏,无论它们是否实际使用信号,插槽和属性,因为如果不这样做可能会导致某些函数表现出奇怪的行为。

Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.

这篇关于QTimer :: singleShot()在给定对象的父类中查找指定的槽,而不是对象本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆