QMessage box signal and slot

  • is there any way to connect QMessageBox ok button clicked signal to a slot in another class

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    If you are not using the static methods, then yes it is

  • hi and thank you,I wanted to call a slot in another class when ever QMessageBox Yes Button is clicked and I am not using static methods.Icouldnt figure out how

  • Lifetime Qt Champion

    Use QMessageBox::button to retrieve the correct button and connect it accordingly

  • I tried like this but didnt work

  • Hi and welcome to devnet,

    In that case I suggest to use signal forwarding

    1. create a SIGNAL in the anotherClass
    2. connect the message box signal to that signal
    3. connect the first signal to repaint
    // AnotherClass.h
    class QMessageBox;
    class AnotherClass 
        void messageBoxOkClicked();
        QMessageBox *deleteMsgBox;
        deleteMsgBox = new QMessageBox(this);
        connect(deleteMsgBox.button(QMessageBox::Yes), SIGNAL(clicked()), this, SIGNAL(messageBoxOkClicked()));
    connect (anotherClass, SIGNAL(messageBoxOkClicked()), this, SLOY(repaint()));

  • I tried this but it didnt work ,How can I connect two SIGNALS

  • you can connect 2 signals how I wrote.

    can you post your code??

  • anotherclass.h

    QMessageBox deleteMsgBox;


    deleteMsgBox.setText("Deleting state...");
    deleteMsgBox.setInformativeText("Are you sure you want to delete this state?");
    deleteMsgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);

    ret = deleteMsgBox.exec();
    if(ret == QMessageBox::Yes){



Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.