Reputation: 25413
I want to declare my own enum in a class in Qt and use it for signals and slots, but I get this error.
QObject::connect: Cannot queue arguments of type 'ClassA::MyEnum'
(Make sure 'ClassA::MyEnum' is registered using qRegisterMetaType().)
This is my source code:
ClassA.h
public:
enum MyEnum {
READING = 0,
STOPPED = 1,
FINISHED = 2
};
signals:
void changed(QString text, int readTextInPercent, ClassA::MyEnum status);
ClassA.cpp
emit changed(QString("string"), 50, ClassA::READING);
ClassB.h
public slots:
void changed(QString text, int readTextInPercent, ClassA::MyEnum status);
ClassB.cpp
this->connect(m_ClassA, SIGNAL(changed(QString, int, ClassA::MyEnum)), this, SLOT(changed(QString, int, ClassA::MyEnum)));
void ClassB::changed(QString text, int readTextInPercent, ClassA::MyEnum status) {
}
I don't know where and with which parameter I have to put the qRegisterMetaType
.
Upvotes: 3
Views: 4051
Reputation: 9087
You can avoid having to call Q_DECLARE_METATYPE
and qRegisterMetaType()
by using Q_ENUM, which was added in Qt 5.5:
public:
enum MyEnum {
READING = 0,
STOPPED = 1,
FINISHED = 2
};
Q_ENUM(MyEnum)
Upvotes: 2
Reputation: 488
The error thrown by Qt is very descriptive:
Make sure 'ClassA::MyEnum' is registered using qRegisterMetaType()
So you will need to include this line:
qRegisterMetaType<ClassA::MyEnum>("ClassA::MyEnum");
In any part of your code in which you know it will be called. I use to include the qRegisterMetaType in the main function of my applications. I recommend you to take a look to the Qt Documentation about the topics: qRegisterMetaType & Q_DECLARE_METATYPE.
Upvotes: 4
Reputation:
You have to do:
Q_DECLARE_METATYPE(ClassA::MyEnum)
in your classA.h header.
Then in ClassA constructor (or main() but remember to include classa.h there first):
qRegisterMetaType<ClassA::MyEnum>("ClassA::MyEnum");
Then use like:
connect(whatever, SIGNAL(whatever_uses_myenum(ClassA::MyEnum)), ..., ...)
Upvotes: 7