QTimer::singleShot() looks for the specified slot in the given object's parent class, not the object itself

强颜欢笑 提交于 2019-12-04 03:57:43

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).

(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.)

For reference, see the QObject docs:

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.

You need to create a moc file, which is created with qmake if you put Q_OBJECT macro in your class.

So, to fix your example, you need your class changed to this :

class TestApp: public QApplication {
    Q_OBJECT
    public:
    TestApp(int &argc, char **argv);
    public slots:
    void timeout();
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!