topic logo
Qt ⇢ Unit 4

Message Boxes

information, warnings, criticals and questions

Summary

Demonstrate all four message box types.

Background

Unit 2 introduced the message box and noted that there are four basic types. This unit introduces them and shows how to apply costomizations.

New Classes

  • QIcon: An icon that can be used as part of a button label, menu label, listing or as a window icon.

Popups App

The sample application is similar to the last one, but instead of labels the layout contains buttons that are connected to new slots, each of which showing a message box with some variations.

Five Buttons To Trigger Message Boxes
Five Buttons To Trigger Message Boxes

Five Slots

This unit skips the listing of the header which declares the slots that are connected to the buttons. The implementation follows:

popups.cpp
void PopupMsgs::showInfo()
{
    QMessageBox::information(this, "Info", "Just so you know..");
}

void PopupMsgs::showWarning()
{
    QMessageBox::warning(this, "Warning!", "Something is happening!");
}

void PopupMsgs::showCritical()
{
    QMessageBox::critical(this, "Critical!!",
        "Run for the emergency equipment!!");
}

void PopupMsgs::showQuestion()
{
    auto answer = QMessageBox::question(this, "Question?",
        "Are you honestly shure you want to do that?");
    if (answer == QMessageBox::Yes) {
        QMessageBox::information(this, "Info", "You asked for it..");
    }
}

These slots are connected to the buttons. The first one is the information popup that was also featured in unit 2. The second and third slot show the warning and critical message boxes. They take the same arguments. The difference lies in the icon that they display in the pop-up messages.

The fourth message box is different from the others. It’s a question and has two answer options by default: [Yes] and [No]. The answer to this can be evaluated to alter the following program flow.

popups.cpp
void PopupMsgs::showCritical2()
{
    auto answer = QMessageBox::critical(this, "What to do..",
        "We are heading for the wall, what do we do?",
        QMessageBox::Abort|QMessageBox::Ignore|QMessageBox::Help,
        QMessageBox::Abort);

    switch (answer) {
        case QMessageBox::Abort:
            // change course
            break;
        case QMessageBox::Help:
            // show a typical crash result
            break;
        default:
            break;
    }
}

The last slot triggers a costomized critical dialog. This shows the same icon as before, but instead of one button it has three: [Abort], [Ignore] and [Help]. The choice can be evaluated in the same way as the question message box.

Configuring Message Box Buttons
Configuring Message Box Buttons

The buttons are placed in accordance to the idioms of the platform the application runs on, e.g. they are placed differently on Linux, OS X, etc.

The fifth optional argument sets the button that is selected by default. This is the button that is activated when pressing the [Spacebar] button instead of using the mouse to interact with the dialog. There is a highlighting outline around this button.

Initialization

The constructor is a re-hash of the things introduced in the previous units. There is a minor new method call though:

setWindowIcon(QIcon::fromTheme("info"));

“QWidget::setWindowIcon(icon)” sets the icon that is shown in the window decoration and the system task selector. An icon can be created from an image file with the “QIcon::addFile(filename)” method or loaded from the theme if the deployment supports this.

Conclusion

This unit has shown a few options when it comes to message boxes. Some applications use these to communicate special events with the user, for example to ask if a document should be saved before closing the application.