Important: Please read the Qt Code of Conduct -

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."));

    I get the following error message:
    C:\Programming\Projects\Folkfriends_1\additem.cpp:82: error: expected primary-expression before ',' token
    Please help me to figure out what's wrong.
    Thank you.

  • Qt Champions 2019

    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?

  • 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.

  • Lifetime Qt Champion

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

    so try

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

    void thedialog::Myreject(QAbstractButton *) {

  • 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)

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

  • Thank you.

Log in to reply