I wanted to know if it is possible to specify multiple connection types. For example, I want my connection type to be a queued connection and a unique connection. Is it possible to specify that in one statement ?
QObject::connect(ptrSender,SIGNAL(..),ptrReceiver,SLOT(...),Queued-and-unique)
Update :
Following the suggestion of the posters:
I tried using Qt::QueuedConnection | Qt::UniqueConnection
but I get
`Error 1 error C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 5 from 'int' to 'Qt::ConnectionType'
It's possible, but C++ prevents passing the OR in the way you're doing it.
The problem is that connect
takes an argument of type Qt::ConnectionType
. Bitwise OR'ing two values from an enum produces a value of type int
(the enum values undergo integer promotion in order to apply the bitwise OR operator). This causes the compile error you're getting:
cannot convert parameter 5 from 'int' to 'Qt::ConnectionType'
(Remember that in C++ integrals don't automatically convert into enums).
So the solution is casting back the result of the OR into the right type, and the right cast to use is static_cast
:
static_cast<Qt::ConnectionType>(Qt::QueuedConnection | Qt::UniqueConnection)
Now you might ask "why didn't Qt authors think about this"? Well, the fact is that Qt::UniqueConnection
has been added in Qt 4.6.
Before, connect
only took a single enum value, not an OR combination of them. That's why connect
has this signature:
connect(..., Qt::ConnectionType)
and not something like this:
connect(..., QFlags<Qt::ConnectionType>)
connect(..., int)
which instead would've allowed the OR. (Note that these latter two signatures also allow for things such as Qt::DirectConnection | Qt::QueuedConnection
, which don't make sense).
Is it possible to specify that in one statement ?
In theory, it seems to be possible for your scenario. At least, you can read the docs documentation about it.
Qt::UniqueConnection 0x80 This is a flag that can be combined with any one of the above connection types, using a bitwise OR. When Qt::UniqueConnection is set, QObject::connect() will fail if the connection already exists (i.e. if the same signal is already connected to the same slot for the same pair of objects). This flag was introduced in Qt 4.6.
Based on the aforementioned documentation, you would write something like this:
#include <QCoreApplication>
int main(int argc, char **argv)
{
QCoreApplication coreApplication(argc, argv);
QObject::connect(&coreApplication, SIGNAL(aboutToQuit()), &coreApplication, SLOT(quit()), static_cast<Qt::ConnectionType>(Qt::QueuedConnection | Qt::UniqueConnection));
return coreApplication.exec();
}
Sure, it's possible. Because Qt::UniqueConnection
is a flag that can be combined with any one of other connection types, using a bitwise OR.
QObject::connect(ptrSender,SIGNAL(..),ptrReceiver,SLOT(...), Qt::UniqueConnection | Qt::QueuedConnection);
Check documentation: connection type
In Qt v. 4.8 Qt::UniqueConnection
also has value 0x80, which allows to combine it with other connection types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With