📜  qt messagebox - C++ (1)

📅  最后修改于: 2023-12-03 14:46:50.462000             🧑  作者: Mango

Qt MessageBox - C++

Introduction

Qt MessageBox is a class in the Qt framework that provides a way to display a pop-up message or a modal dialog box to the user. This is a simple way to prompt the user with messages or to ask for confirmation before proceeding.

Functionality

The MessageBox class provides several functions for creating windows that show different types of messages. Some of the functions are:

  • information: displays an information message;
  • question: displays a question message;
  • warning: displays a warning message;
  • critical: displays a critical error message.
Usage

To use a Qt MessageBox in your application, you need to include the QMessageBox header file:

#include <QMessageBox>

Then, you can use one of the functions to create the desired message box. For example:

QMessageBox::information(this, tr("Title"), tr("Message text."));

This will display a modal message box with an information icon, a title "Title" and the message "Message text." You can also add buttons to the message box and detect which button was clicked by using the exec() function:

QMessageBox msgBox;
msgBox.setText("Message text.");
msgBox.setInformativeText("Informative text.");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();

if (ret == QMessageBox::Ok) {
    // OK button was clicked
} else if (ret == QMessageBox::Cancel) {
    // Cancel button was clicked
}

This code creates a message box with a message and an informative text, and two buttons: "OK" and "Cancel". The default button is "OK". The exec() function shows the message box and returns the button that was clicked.

Conclusion

The Qt MessageBox provides a simple way to display messages and ask for user confirmation. It is easy to use and customizable, allowing you to create different types of message boxes depending on your needs.