What types can you pass as signal args on QT ?
-
What types can you pass as signal args on QT ?
-
I think you can pass anything. I never bothered to check, but it always worked :)
-
Did you passes enums ?
-
I don't think I have tried that. But enums are ints, so there should not be any problems, especially if you wrap them as QFlag/ QFlags.
-
How do you wrap them ?
-
QFlags introduces some nice features, like more rigid compile-time checks and easy int conversion. Here is the doc for that: "QFlags":http://qt-project.org/doc/qt-4.8/qflags.html.
-
you can pass anything as arguments as long as the types are registered using "qRegisterMetaType":http://qt-project.org/doc/qt-4.8/qmetatype.html#qRegisterMetaType and "Q_DECLARE_METATYPE":http://qt-project.org/doc/qt-4.8/qmetatype.html#Q_DECLARE_METATYPE
note that using pointers and/or references as arguments might have unforseen side-effects:
If you're passing a pointer or reference to a local instance to a queued signal-slot connection, you might end up with an invalid argument in the slot since the original instance might already be gone by the time the slot is called. -
Can I register just an ENUM from some QT class or do I have to register the all class ?
-
It's possible to register just the enum. If you're registering it outside the class, you will have to use the fully qualified path (enum name including all the namespace/class names, e.g. "CMyClass::EMyEnum"). I'm not sure what the behaviour is if you're registering the enum within the class itself and then emit/invoke outside the class. It's possible that you'll have to register the enum on the same level you're using it from (e.g. "EMyEnum" when using it within the class, "CMyClass::EMyEnum" when using it outside CMyClass), but I haven't tried that.
-
Did you ever tried pass QT class enum as signal parameter ?
-
I think I did, but I can't tell with absolute certainty.
If you can't get it to work, you could try moving the enum from within the class to its own namespace. -
The enum is part of a QT class
-
I did use enums as signal/slot parameters and generally you have two easy solutions:
- Use Q_ENUMS() to register your enum in the class you want to use
- use the mentioned qRegisterMetaType() to register it as a meta-type for Qt
I prefer the 2nd solution, it's more error-proof in most of the cases.
-
I mean that the enum I want to use is part of a Qt class that was provided by Qt (not a class I created)
-
Qt defined enums are nothing special. The same rules apply for them.
Before you could connect a signal/slot with an enum parameter you have to register it (either with qRegisterMetaType() or with Q_ENUMS). Qt may register the enum you want to use (which is part of Qt) but may not, or will register it later when it uses somewhere but you would like to use it before Qt would registers it.
So the principle is you should register any enum you want to use before you would actually use it. It doesn't matter whether it's your own or it's part of Qt.
Of course you can experiment as well -> if Qt cannot use it in a signal/slot it will print an error message for you in the log and will point you to the exact location where it cannot handle it.
-
Thank you.
I will try it