Using MessageBox buttonclicked signal



  • Hi,
    I want to use the signal emitted by a messageBox to close a QDialog.

    I have the following code:

     QMessageBox::information (this, tr("Confirmation"), tr("<b><font size = '16' color = 'green'>The Friend was added to the database."));
     connect(QMessageBox,SIGNAL(buttonClicked(Ok)),this,SLOT(reject()));
    

    I get the following error message:
    C:\Programming\Projects\Folkfriends_1\additem.cpp:82: error: expected primary-expression before ',' token
    connect(QMessageBox,SIGNAL(buttonClicked(Ok)),this,SLOT(reject()));
    ^
    Please help me to figure out what's wrong.
    Thank you.


  • Moderators

    Signals/slots don't work this way. You can only connect signals/slots using instances not just classes like you're trying. You have to instantiate QMessageBox:

    QMessageBox msgBox(this, tr("Confirmation"), tr("<b><font size = '16' color = 'green'>The Friend was added to the database."));
    // What is Ok in your example?
     connect(msgBox,SIGNAL(buttonClicked()),this,SLOT(reject()));
    msg.exec();
    


  • Thank you.
    I tried this way:

    connect(&msgBox, SIGNAL(buttonClicked()),this,SLOT(reject()));
    

    and I got the following error message:
    take the address of the argument with &

    So I changed it for this:

    connect(&msgBox, SIGNAL(buttonClicked()),this,SLOT(reject()));
    

    This way there is no error message, but it doesn't do anything. Please help me to figure what else to add. Thank you.


  • Qt Champions 2016

    @gabor53
    hi
    is it not
    void QMessageBox::buttonClicked(QAbstractButton * button)

    so try

    qDebug() << connect(&msgBox, SIGNAL(buttonClicked(QAbstractButton *)),this,SLOT(MYreject(QAbstractButton *)));

    void thedialog::Myreject(QAbstractButton *) {
    reject();
    }


  • Moderators

    This is a very contrived way of using a message box.
    The way it's meant to be used is something like:

    //taken out just to shorten the lines
    QString title = tr("Confirmation");
    QString msg = tr("<b><font size = '16' color = 'green'>The Friend was added to the database.");
    
    if (QMessageBox::information (this, title, msg) == QMessageBox::Ok)
       reject();
    

    ...although it's a little weird that you call reject() when user confirmed something.



  • Thank you.


Log in to reply
 

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