Home > Net >  Can't use Qt::UniqueConnection in Qt connect
Can't use Qt::UniqueConnection in Qt connect

Time:07-06

I have a connect statement that compiles fine, with 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));

CodePudding user response:

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 enter image description here

CodePudding user response:

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

  • Related