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
    connect(anotherclass->deleteMsgBox.button(QMessageBox::Yes),SIGNAL(clicked()),this,SLOT(repaint()));



  • 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 
    {
    Q_SIGNALS:
        void messageBoxOkClicked();
    
    private:
        QMessageBox *deleteMsgBox;
    };
    
    //AnotherClass.cpp
    AnotherClass::AnotherClass() 
    {
        deleteMsgBox = new QMessageBox(this);
    
        connect(deleteMsgBox.button(QMessageBox::Yes), SIGNAL(clicked()), this, SIGNAL(messageBoxOkClicked()));
    }
    
    //Widget.cpp
    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;

    anotherclass.cpp:QGraphicsRectItem

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

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

    }
    mainclass.cpp{

    mainclass::make()
    {
    connect(anotherclass->deleteMsgBox.button(QMessageBox::Yes),SIGNAL(clicked()),this,SLOT(repaint()));
    }
    mainclass::repaintAll()
    {
    ..............
    }
    }


Log in to reply
 

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