In the next few examples we will look at how to use several different static methods provided by the CopperSpice GUI library.
In our prior examples we used QColorDialog::getColor() to display a color palette. GetColor() is a static method in the QColorDialog class. A static method is a special category of methods which can be called without creating an object of the class. This example will introduce a few message box static methods.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void infoMsg();
void questionMsg();
void warnMsg();
QLineEdit *info_text;
QLineEdit *quest_text;
QLineEdit *warn_text;
};
The declaration for MainWindow declares three slot methods and then three line edit widgets which will be updated by the corresponding slot methods.
MainWindow::MainWindow()
{
setMinimumSize(700, 350);
QPushButton *info_pb = new QPushButton("Information");
info_text = new QLineEdit();
QPushButton *quest_pb = new QPushButton("Question");
quest_text = new QLineEdit();
QPushButton *warn_pb = new QPushButton("Warning");
warn_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(info_pb, 0, 0);
grid1->addWidget(info_text, 0, 1);
grid1->addWidget(quest_pb, 1, 0);
grid1->addWidget(quest_text, 1, 1);
grid1->addWidget(warn_pb, 2, 0);
grid1->addWidget(warn_text, 2, 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(info_pb, &QPushButton::clicked,
this, &MainWindow::infoMsg);
connect(quest_pb, &QPushButton::clicked,
this, &MainWindow::questionMsg);
connect(warn_pb, &QPushButton::clicked,
this, &MainWindow::warnMsg);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
The first part of this code creates three push buttons, where each one is followed by a line edit widget. Clicking the buttons will display a message box with some information, question, or warning message. The result of the message box will be displayed in the corresponding edit located to the right of each push button.
The controls are put into a grid layout and then on line 38 grid1 is added to a vertical layout.
Signal / Slot Connections
Lines 43, 46, and 49 set up what action should be taken when each of the three push buttons are clicked.
void MainWindow::infoMsg()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::information(this, "Information",
"Body of informative message. "
"Click OK, X icon, or press Esc");
// since no buttons were passed, reply can only be OK
info_text->setText("OK");
}
void MainWindow::questionMsg()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Question",
"Body of a question. "
"Click one of the buttons, X icon, or press Esc",
QMessageBox::Yes |
QMessageBox::No |
QMessageBox::Cancel);
if (reply == QMessageBox::Yes) {
quest_text->setText("Yes");
} else if (reply == QMessageBox::No) {
quest_text->setText("No");
} else {
quest_text->setText("Cancel");
}
}
void MainWindow::warnMsg()
{
QMessageBox msgBox(QMessageBox::Warning, "Warning",
"Body of a warning message. "
"Click one of the buttons, X icon, or press Esc",
Qt::EmptyFlag, this);
msgBox.addButton("Save", QMessageBox::AcceptRole);
msgBox.addButton("Ignore", QMessageBox::RejectRole);
if (msgBox.exec() == QMessageBox::AcceptRole) {
warn_text->setText("Save");
} else {
warn_text->setText("Ignore");
}
}
In our last block of code on line 5 we are calling QMessageBox::information(). This is a static method which is part of the QMessageBox class. We can pass various parameters to indicate the title of the information message box and the message. This method is sort of unique since there are very few buttons you can display.
On line 17 is a call to static method QMessageBox::question(). The passed parameters indicate the Yes, No, and Cancel buttons should be displayed. After the user presses a key we can test what button they pressed. If they press ESC or the X in the upper right corner then we return “Cancel”.
The last slot method is a bit different. Line 37 constructs a QMessageBox and then sets up everything by hand. This approach gives developers more options. For example, lines 42 and 43 call addButton() to declare user defined push buttons and what meaning they should have.
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_9”.