Enum Signal Parameter

I am trying to use the Qt signal-and-slots mechanism with custom enum types.

I read all of the following, and none of this helped:

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

At runtime, I get the following messages (in the application output panel in Qt Creator):

Object :: connect: There is no such signal
DetectorEngineThread :: statusChanged (DetectorEngineThread :: Status) in ...
Object :: connect: no such signal
DetectorEngineThread :: processingError (DetectorEngineThread :: ErrorCode) in ...

And, obviously, the slot code never starts, even though the corresponding signals are emitted.

I tried:

  • moving transfers to a global scope, but the problem remains.
  • with signals and slots connected automatically through QMetaObject :: connectSlotsByName, but this met the same problem.
  • using local names (for example, Status instead of DetectorEngineThread :: Status) in qRegisterMetaType and Q_DECLARE_METATYPE, and also tried to use them in SIGNAL and SLOT macros.
+4
source share
1 answer

Enumerations declared in signals and slots should be fully defined as follows:

 void statusChanged(Status newStatus); void processingError(ErrorCode code); 

Must be:

 void statusChanged(DetectorEngineThread::Status newStatus); void processingError(DetectorEngineThread::ErrorCode code); 
+9
source

Source: https://habr.com/ru/post/1443866/


All Articles