1. Introduction
When performing some irreversible operations on the computer, the computer usually sends a pop-up window asking us whether we feel like we are continuing this operation, and we can choose to continue or cancel. So is there any class that can implement similar functions in Qt? Some brothers, some! We have a class like thisQMessageBox
!
2. Brief introduction
QMessageBox
Yes used in Qt to pop upDialog messageThe class is often used in scenarios such as prompting information, warnings, errors, and confirmation. It is one of the most commonly used components in Qt GUI application development.
It is used to pop up a mode dialog box (modal window), display a message to the user, and wait for the user to click the button (such as "OK", "Cancel", "Yes", "No", etc.) before continuing the program execution.
3. Common functions
Qt provides very fast static functions, which are very convenient to use!
Information box
QMessageBox::information(this, "title", "This is a message box");
Warning box
QMessageBox::warning(this,"warn","This is a warning box");
Error box (critical)
QMessageBox::critical(this,"mistake","Serious error occurred");
Question box (question)) (with button option)
int ret = QMessageBox::question(nullptr, ("MyNoteBook Notice:"), ("The document has been modified.\n" "Do you want to save your changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, /*Properties of button*/ QMessageBox::Save); /*Default button press*/ switch (ret) { case QMessageBox::Save: qDebug()<<"QMessageBox::Save"; break; case QMessageBox::Discard: qDebug()<<"QMessageBox::Discard"; break; case QMessageBox::Cancel: qDebug()<<"QMessageBox::Cancel"; break; default: break; }
4. Button type (QMessage::StandardButton)
Button enumeration value | Show content |
---|---|
QMessageBox::Ok | "Sure" |
QMessageBox::Cancel | "Cancel" |
QMessageBox::Yes | "yes" |
QMessageBox::No | "no" |
QMessageBox::Abort | "termination" |
QMessageBox::Retry | "Try again" |
QMessageBox::Ignore | "neglect" |
Can be used in combination with multiple buttons
`QMessageBox::Ok`|`QMessageBox::Cancel`
Icon Type | meaning |
---|---|
QMessageBox::NoIcon | No icon |
QMessageBox::Information | Information icon (ℹ️) |
QMessageBox::Warning | Warning icon (⚠) |
QMessageBox::Critical | Error icon (❌) |
QMessageBox::Question | Question mark icon (❓) |
5. Implement pop-up windows in steps
// Create a QMessageBox message box object to prompt the user whether to save the file QMessageBox Box; // Set the dialog window title ("MyNoteBook Notice:"); // Set the text displayed in the dialog box (\n means line break) ("The document has been modified.\n" "Do you want to save your changes?"); // Set the buttons on the dialog box to two options: "Yes" and "No" (QMessageBox::Yes | QMessageBox::No); // Set the default selected button to "Yes" (QMessageBox::Yes); // A dialog box pops up and waits for the user to click the button to return the button value (Yes or No) that the user clicks. int ret = (); // Perform different operations according to the button clicked by the user switch (ret) { case QMessageBox::Yes: // The user clicked "Yes": You can write the logic to save the file here // For example: saveFile(); break; case QMessageBox::No: // The user clicked "No": you can choose not to save, directly exit or continue to close the program break; default: // In theory, it won't be executed here, but the default branch is reserved for security reasons break; }
6. Summary
In this article, we have learned the common class in QT, QMessageBox, and understand how the dialog box appears and how to modify the content of the dialog box!
This is the end of this article about the specific use of Qt QMessageBox. For more related Qt QMessageBox content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!