Alex Rebell
Alex Rebell

Reputation: 374

how it works QMessageBox

Please tell me where I made a mistake? My code:

void deletetable::on_pb_dell_clicked()
{
    QMessageBox messageBox(QMessageBox::Question,
            tr("Sure want to quit?"), tr("Sure to quit?"), QMessageBox::Yes | QMessageBox::No, this);
    messageBox.setButtonText(QMessageBox::Yes, tr("Yes"));
    messageBox.setButtonText(QMessageBox::No, tr("No"));
    messageBox.exec();
    if (messageBox.QMessageBox::Yes) {
        emit deleteYear(year);
        close();
    } else {
        
    }
}

my function deleteYear(year) works in any condition, i.e. if I click "No", the function will still work. I took an example from here https://stackoverflow.com/a/31533126/13023647

Upvotes: 1

Views: 1134

Answers (1)

0RR
0RR

Reputation: 1603

messageBox.QMessageBox::Yes

Is just accessing the enum Yes, which is going to evaluate the same each time.

You want to capture the actual response from the question and query that, such as:

auto response = QMessageBox::question(this, "Save", "Do you wish to save?", QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);


if (response == QMessageBox::Save) { ... }

See https://doc.qt.io/qt-5/qmessagebox.html#question here for more info.


To keep the same format above you can get the response with messageBox.result() e.g.

if (messageBox.result() == QMessageBox::Yes) { ... }

Upvotes: 3

Related Questions