0

I have a connect statement that compiles fine, with the connection type QueuedConnection. Once I OR it with UniqueConnection, it refuses to compile. I had to re-cast it back to Qt::ConnectionType for it to run. Is this normal/expected behaviour?

I thought the connection method definition would accept an int (which is equivalent to enum) without complaint.

connect(
    mySource, SIGNAL(mySig()),
    this, SLOT(mySlot())),
    static_cast<Qt::ConnectionType> 
    (Qt::QueuedConnection|Qt::UniqueConnection));
rgettman
  • 176,041
  • 30
  • 275
  • 357
TSG
  • 4,242
  • 9
  • 61
  • 121
  • 1
    My eyes start bleeding everytime I see the old style of connection using SIGNAL/SLOT macros. :) I wish there were less examples of this everywhere on the internet because it uses newbies the **wrong** style. Nevertheless I admit the question is not affected by this. – HiFile.app - best file manager Jul 06 '22 at 07:14
  • 1
    Already answered here https://stackoverflow.com/questions/20717526/qt-specifying-multiple-connection-types-with-qobjectconnect To sum up: yes, you can combine the flags but you need to cast from int to proper enum type. – HiFile.app - best file manager Jul 06 '22 at 07:20
  • as @HiFile.app-bestfilemanager mentioned, I try `static_cast(Qt::QueuedConnection | Qt::UniqueConnection)` in my example and it works without any error so what is your problem? I didn't get it. From your question, the question arose for me, what is the reason for your use of ‍‍`Qt::UniqueConnection` with `Qt::QueuedConnection`? – Parisa.H.R Jul 06 '22 at 07:49
  • 1
    @Parisa.H.R UniqueConnection ensures (runtime check) that the connection is made only once for each pair of signals and slots. So it can be of course combined with queued connection because it does something orthogonal - it makes invocation asynchronous and it allows queuing of multiple invocations. Regarding UniqueConnection, I personally think that it is a big mistake in Qt that UniqueConnection is not the default in Qt with the option to request non-unique ones. I tried to add a feature request https://bugreports.qt.io/browse/QTBUG-102741, hoepfully we will see it in Qt7. :) – HiFile.app - best file manager Jul 06 '22 at 08:23

2 Answers2

-1

I try this Example :

QPushButton *button = new QPushButton();
button->setText("Clicke me ");
ui->gridLayout->addWidget(button);

connect(button,&QPushButton::clicked,this,[](){
qDebug()<<"button is clicked";
}, Qt::UniqueConnection);

what you write In your connect is wrong, because you should follow this style :

connect(object,SIGNAL(mySig()), this,SLOT(mySlot())),Qt::QueuedConnection); 

you can't use two Connections at the same time

enter image description here

From Qt::ConnectionType for Qt::UniqueConnection :

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.

Noted that it was introduced in Qt 4.6 and it said that if the connection already exists QObject::connect() will fail.

as I try

  connect(button,&QPushButton::clicked,this,[](){
    qDebug()<<"button is clicked";
    }, Qt::UniqueConnection or Qt::QueuedConnection);

I got an error too because I use the Qt 6 version and it may work in Qt 4.6.

From https://stackoverflow.com/a/20717599/9484913 I try this :

QPushButton *button = new QPushButton();
button->setText("Clicke me ");
ui->gridLayout->addWidget(button);

QObject::connect(button,&QPushButton::clicked,this,[](){
qDebug()<<"button is clicked";
}, static_cast<Qt::ConnectionType>(Qt::QueuedConnection | Qt::UniqueConnection));

and it works:

enter image description here

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
  • I think you missed the point of the question...the "solution" you came up with is already part of the question. The question asks why is casting back to ConnectionType necessary - should an int be allowed if Qt specifies flags can be OR'ed together – TSG Jul 06 '22 at 12:16
  • @TSG Because we are trying it in qt 6 , but it interduced in qt 4 . Qt doesn't have backward compatibility – Parisa.H.R Jul 06 '22 at 17:59
  • We should have check codes of Qt development from that time until now to understand why now need to cast it . But in question you said is this normal or not ? I said it is normal :) – Parisa.H.R Jul 06 '22 at 18:18
-2

You have to explicitly choose one of the connection types, you cannot have multiple in one connection.

Check the doc for their differences: Qt connections types

parti82
  • 165
  • 5
  • 3
    Read docs here https://doc.qt.io/qt-6/qt.html#ConnectionType-enum About `UniqueConnection`: "This is a flag that can be combined with any one of the above connection types, using a bitwise OR. ..." – HiFile.app - best file manager Jul 06 '22 at 06:49