Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

c++ - 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

question from:https://stackoverflow.com/questions/65881568/how-it-works-qmessagebox

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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) { ... }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...