Search code examples
c++qtqobjectqtcoreqt-signals

Qt: Specifying multiple connection types with QObject::connect


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'

Solution

  • 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();
    }