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

13,245

Solution 1

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.

Solution 2

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();
};
Share:
13,245
rhino
Author by

rhino

I like learning programming-related technologies and I'm really satisfied when I can answer someone's question accurately. My native language is Polish, but I've been learning English for quite a few years now and my language skills are constantly improving. I am a Windows user, but I have some basic experience with various GNU/Linux systems.

Updated on June 13, 2022

Comments

  • rhino
    rhino almost 2 years

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

    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();
    }
    

    This is what app.h looks like:

    #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
    

    And this is app.cpp:

    #include "app.h"
    
    #include <QDebug>
    
    TestApp::TestApp(int &argc, char **argv): QApplication(argc, argv) {
    }
    
    void TestApp::timeout() {
        qDebug() << "timeout called";
    }
    

    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.

  • Melebius
    Melebius over 9 years
    +1 for mentioning the moc file! Putting Q_OBJECT into the class declaration is insufficient when Qmake is not called, for example when building with CMake.
  • BЈовић
    BЈовић over 9 years
    @Melebius For that, you need to use QT4_WRAP_CPP, so the cmake would parse headers, and generate moc files.