Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connect MessageBox buttonClicked() signal does not work



  • Hi

    In MyClass, i have a messageBox pointer member variable and in MyClass constructor:

    msgBox = new QMessageBox(QMessageBox::Information, tr("Request"),
                                            "Waiting ...", QMessageBox::Cancel, this);
    

    when it is needed to open the message box:

    msgBox->open(this, SLOT(waitReplyCancelled(QAbstractButton*))); // connect MessageBox::buttonClicked() to slot
    
    

    I need the control to return immediately back to the main loop so I did not use exec(), etc. It works just fine. When I clicked the Cancel button, the message box closed and the following slot was called. No problem.

    void MyClass::waitReplyCancelled(QAbstractButton* buttonClicked) {
        qDebug() << "I'm waitReplyCancelled(QAbstractButton*)";
    }
    

    Okay now I want to manually connect the slot as I don't want every time the message box is opened it need to connect the signal again like in open(). So I use show() instead and try to connect to the signal myself. So in MyClass constructor:

    msgBox = new QMessageBox(QMessageBox::Information, tr("Request"),
                                            "Waiting ...", QMessageBox::Cancel, this);
    connect(msgBox, SIGNAL(buttonClicked(QAbstractButton *button)), this, SLOT(waitReplyCancelled(QAbstractButton *button)));
    msgBox->setModal(true);
    

    When the message box is needed, I just show() the message box:

    msgBox->show();
    

    Unfortunately, this way does not work. When I clicked the Cancel button, the message box closed but the slot was not called.

    Anyone can help me?


  • Moderators

    @dalishi said in Connect MessageBox buttonClicked() signal does not work:

    connect(msgBox, SIGNAL(buttonClicked(QAbstractButton *button)), this, SLOT(waitReplyCancelled(QAbstractButton *button)));

    That's not a valid connect syntax, probably the problem.


  • Qt Champions 2019

    @dalishi Please check the application output - there is probably a warning saying that the connect() did not work.



  • @J.Hilk Thanks. The following works without parameter names.

    connect(msgBox, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(waitReplyCancelled(QAbstractButton *)));
    


  • @jsulm Hi Thanks. I must be fool to ignore that warning....


Log in to reply