我正在尝试使用Qt的信号和插槽机制以及自定义枚举类型。
我已阅读以下所有内容,但没有任何帮助:
DetectorEngineThread.h:
class DetectorEngineThread : public QThread
{
Q_OBJECT
Q_ENUMS(ErrorCode)
Q_ENUMS(Status)
public:
enum ErrorCode
{
...
};
enum Status
{
...
};
...
signals:
void statusChanged(Status newStatus);
void processingError(ErrorCode code);
};
Q_DECLARE_METATYPE(DetectorEngineThread::ErrorCode)
Q_DECLARE_METATYPE(DetectorEngineThread::Status)
MainWindow.h:
...
#include "DetectorEngineThread.h"
...
class MainWindow : public QMainWindow
{
Q_OBJECT
...
private:
DetectorEngineThread* m_detEng;
...
private slots:
void on_detEng_statusChanged(DetectorEngineThread::Status newStatus);
void on_detEng_processingError(DetectorEngineThread::ErrorCode errorCode);
...
};
MainWindow.cpp:
...
#include "MainWindow.h"
...
MainWindow::MainWindow(...) : ...
{
...
qRegisterMetaType<DetectorEngineThread::Status>("DetectorEngineThread::Status");
qRegisterMetaType<DetectorEngineThread::ErrorCode>("DetectorEngineThread::ErrorCode");
...
m_detEng = new DetectorEngineThread(...);
connect(m_detEng, SIGNAL(statusChanged(DetectorEngineThread::Status)),
this, SLOT(on_detEng_statusChanged(DetectorEngineThread::Status)), Qt::QueuedConnection);
connect(m_detEng, SIGNAL(processingError(DetectorEngineThread::ErrorCode)),
this, SLOT(on_detEng_processingError(DetectorEngineThread::ErrorCode)), Qt::QueuedConnection);
...
}
...
void MainWindow::on_detEng_statusChanged(DetectorEngineThread::Status newStatus)
{
...
}
void MainWindow::on_detEng_processingError(DetectorEngineThread::ErrorCode errorCode)
{
...
}
...
在运行时,我收到以下消息(在Qt Creator的Application Output面板中):
对象::连接:没有这样的信号
DetectorEngineThread :: statusChanged(DetectorEngineThread :: Status)in ...
Object :: connect:没有这样的信号
在...中检测到ExengEngineThread :: processingError(DetectorEngineThread :: ErrorCode)
显然,插槽代码永远不会运行,尽管发出了匹配信号。
我试过了:
答案 0 :(得分:9)
在信号和插槽中声明的枚举应完全合格,以便:
void statusChanged(Status newStatus);
void processingError(ErrorCode code);
应该是:
void statusChanged(DetectorEngineThread::Status newStatus);
void processingError(DetectorEngineThread::ErrorCode code);