This example introduces the QErrorMessage class and one additional message box static method.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void errorMsg();
void criticalMsg();
QLineEdit *error_text;
QLineEdit *critical_text;
QErrorMessage *errorDialog = nullptr;
};
The declaration for MainWindow declares two slot methods and two line edit widgets which will be updated by the corresponding slot methods. We want the declaration for errorDialog in the class so we can query if we have already created it in the errorMsg() slot.
MainWindow::MainWindow()
{
setMinimumSize(700, 350);
QPushButton *err_pb = new QPushButton("Error");
error_text = new QLineEdit();
QPushButton *critical_pb = new QPushButton("Critical");
critical_text = new QLineEdit();
//
QPushButton *close_pb = new QPushButton();
close_pb->setText("Close");
QGridLayout *grid1 = new QGridLayout();
grid1->setContentsMargins(75, 45, 75, 25);
grid1->setHorizontalSpacing(20);
grid1->setVerticalSpacing(45);
grid1->addWidget(err_pb, 0, 0);
grid1->addWidget(error_text, 0, 1);
grid1->addWidget(critical_pb, 1, 0);
grid1->addWidget(critical_text, 1, 1);
QHBoxLayout *layout1 = new QHBoxLayout();
layout1->addStretch();
layout1->addWidget(close_pb);
layout1->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->addLayout(grid1);
layoutMain->addSpacing(75);
layoutMain->addLayout(layout1);
connect(err_pb, &QPushButton::clicked,
this, &MainWindow::errorMsg);
connect(critical_pb, &QPushButton::clicked,
this, &MainWindow::criticalMsg);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
The first part of this code creates two push buttons, each followed by a line edit widget. Clicking the buttons will display a message box. The result of the message box will be displayed in the corresponding edit.
The controls are put into a grid layout and then on line 32 grid1 is added to a vertical layout.
Signal / Slot Connections
Lines 37 and 40 set up what action should be taken when one of the two push buttons are clicked.
void MainWindow::errorMsg()
{
if (errorDialog == nullptr) {
errorDialog = new QErrorMessage(this);
}
errorDialog->showMessage(
"If the checkbox remains checked, this message box "
"will be displayed again. \nIf unchecked, this "
"message box will not appear again for the same "
"error message.");
error_text->setText("Message displayed");
}
void MainWindow::criticalMsg()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::critical(this, "Critical",
"Body of a critical message. "
"Click one of the buttons, X icon, or press Esc",
QMessageBox::Abort |
QMessageBox::Retry |
QMessageBox::Ignore);
if (reply == QMessageBox::Abort) {
critical_text->setText("Abort");
} else if (reply == QMessageBox::Retry) {
critical_text->setText("Retry");
} else {
critical_text->setText("Ignore");
}
}
This block of code shows the two slot methods. In the errorMsg() slot we are creating a new QErrorMessage() on line 4, if it does not exist. There is an interesting property of this class. There is a check box on the dialog and if the user enables it, then the next time the errorDialog is displayed the class will check if the message text has already been shown. This means duplicate messages will not be displayed if the user chooses to suppress them.
The criticalMsg() slot calls another static method which is shown on line 20. QMessageBox::critical() works very much like the static methods we showed in the last example.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_10”.