为什么这个线程槽没有被调用?

Why is this thread slot not getting called?

Two.h

#ifndef TWO_H
#define TWO_H

#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>

class Two : public QObject
{
   Q_OBJECT

private:
    QTimer abc;
public:
    QString m_xyz;
    Two();

signals:
    void emitThisSignal( int x, QString &y );

public slots:
    void mySlot();
};


class Controller : public QObject
{
    Q_OBJECT

private:
    Two objTwo;

    QThread objQThread;

    Controller();

public slots:
    void mySlot( int x, QString &y)
    {
        qDebug() << "\nWWWWWWWWWWWWW: " << y;
    }
};

#endif // TWO_H

Two.cpp

#include "two.h"

Two::Two()
{
    m_xyz = "aksja";

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Two::mySlot);
    timer->start(1000);
}

void Two::mySlot()
{
    emit emitThisSignal(4, m_xyz);
    qDebug()<< "FFFFFFFFFFF " << m_xyz;
}

Controller::Controller()
{
    objTwo.moveToThread( &objQThread );

    connect( &objTwo, &Two::emitThisSignal, this, &Controller::mySlot );
    connect( &objQThread, &QThread::finished, &objQThread, &QThread::deleteLater );

    objQThread.start();
}

Controller::~Controller()
{
    delete objTwo;
    objQThread.wait();
}

我可以看到由于 print 语句发出了信号,但是控制器 class 的插槽没有被调用。

void Two::mySlot()
    {
        emit emitThisSignal(4, m_xyz);
        qDebug()<< "FFFFFFFFFFF " << m_xyz;
    }

为什么会这样?

int main( int argc, char* argv[])
{
    QCoreApplication app(argc, argv);
    Controller o;

    return app.exec();
}

参见 QObject::connect, note last argument with default value: Qt::AutoConnection 的文档。

Its documentation says:

(Default) If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used. Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.

现在您陷入了Qt::QueuedConnection场景:

The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread.

所以基本上你需要一些可以提供事件循环的东西。

在此代码中,您需要:

int main( int argc, char* argv[])
{
    QCoreApplication app{argc, argv};
    Controller o;

    // note you need something what will stop this event loop to terminate application
    return app.exec();
}

还有一件事。 现在我注意到您的信号和槽参数很不寻常。问题可能是第二个参数,类型是 QString&.

这可能是问题的根源我不知道 Qt 是否能够编组非 const 引用。如果你将添加 const 那么它将能够编组 QString 并且应该工作(如果我没有错过其他陷阱)。