简体   繁体   中英

Debug Qt slots not called

I wasted hours trying to identify this bug, but I still don't know why it happens... so I resort to you!

I have an object moved to a thread that does some work, when finished emits a signal to be caught by the QMainWindow . As simple as this is, the slot in my window doesn't run. However, if I connect the very same signal to qApp , works:

connect(objInThread_, SIGNAL(mySignal()), this, SLOT(about()));   // Nothing happens

connect(objInThread_, SIGNAL(mySignal()), qApp, SLOT(aboutQt())); // Joy!

Note that I actually copied and pasted the code for this threaded object from another QThread -based object, since I have many of them, and every single slot gets called... except this one.

What can I do to debug this?

Here's the declaration of significant slots:

class mainwin : public QMainWindow
{
     Q_OBJECT

 public:

     mainwin(QWidget *parent = 0, Qt::WindowFlags flags = 0);

     void setupThread()
     {
        // Thread
        objThread_ = new QThread(this);
        objThread_->start();

        // Object in thread
        objInThread_ = new myObject();
        objInThread_->moveToThread(objThread_);

        // Connect
        connect(objInThread_, SIGNAL(mySignal()), this, SLOT(about()));
    }

 // ...

 public slots:

     void loadSettings();

     void about();

 // ...
 }

And the about slot is simply as follows:

void mainwin::about()
{
     qWarning("ABOUT");
}

The threaded object function is the following:

void myObject::fire()
{
    qWarning("this is threaded");

    emit mySignal();
}

Are you sure, that the EventLoop of the thread where the slot functions should be executed is running? In order to process a slot function the corresponding EventLoop has to be running via the exec(...) ( - at least I think it is called exec) function.

Some more clarification: Each Q_OBJECT belongs to a thread. This can be changed by the moveToThread() function. The corresponding thread's event loop has to be running in order to process the slot function.

EDIT: my guess is that your thread (what you created) does not have an eventloop executing.

您确定将mainwin的头文件添加到.pro文件的HEADERS吗?

HEADERS += "Header file of mainwin declaration"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM