Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum signal parameter

I'm trying to use Qt's signals-and-slots mechanism with custom enumeration types.

I have read all the following, and none of it helped:

  • http://doc.qt.digia.com/4.7-snapshot/qtbinding.html#using-enumerations-of-a-custom-type
  • Qt - no such signal error
  • Qt signal with an enum as a parameter

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

During runtime, I get the following messages (in the Application Output panel in Qt Creator):

Object::connect: No such signal
DetectorEngineThread::statusChanged(DetectorEngineThread::Status) in ...
Object::connect: No such signal
DetectorEngineThread::processingError(DetectorEngineThread::ErrorCode) in ...

And obviously the slots' code never run, despite the fact that matching signals are emitted.

I tried:

  • moving the enums into global scope, but the problem remained.
  • having the signals and the slots connected automatically via QMetaObject::connectSlotsByName, but that met with the same issue.
  • using the local names (e.g. Status instead of DetectorEngineThread::Status) in qRegisterMetaType and Q_DECLARE_METATYPE, and also tried to use these in the SIGNAL and SLOT macros.
like image 356
ShdNx Avatar asked Jan 15 '23 03:01

ShdNx


1 Answers

Enums declared in signals and slots should be fully qualified so:

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

Should be:

void statusChanged(DetectorEngineThread::Status newStatus);
void processingError(DetectorEngineThread::ErrorCode code);
like image 165
cmannett85 Avatar answered Jan 25 '23 03:01

cmannett85