A status message can be displayed on the bottom of a GUI main window. If the message is designated as “temporary” or only intended to be shown for a given time interval, it is displayed on the left side of the status bar. If the message is “permanent”, like showing the current line and column number in a document, this will be shown on the right side of the status bar and always visible.
#include <QtCore>
#include <QtGui>
class MainWindow : public QMainWindow
{
public:
MainWindow();
private:
void selectFile();
void showAboutJournal();
};
The class declaration for MainWindow has a public constructor and two private slot methods. This code is identical to example 28.
MainWindow::MainWindow()
{
setMinimumSize(700, 500);
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
// *
QAction *openFile = new QAction("Open", this);
openFile->setShortcuts(QKeySequence::Open);
openFile->setIcon(QIcon("../file_open.png"));
connect(openFile, &QAction::triggered,
this, &MainWindow::selectFile);
QAction *closeFile = new QAction("Close", this);
closeFile->setIcon(QIcon("../file_close.png"));
QAction *saveFile = new QAction("Save", this);
saveFile->setShortcuts(QKeySequence::Save);
saveFile->setIcon(QIcon("../file_save.png"));
QAction *exit = new QAction("Exit", this);
connect(exit, &QAction::triggered,
this, &QWidget::close);
#ifdef Q_OS_WIN
exit->setShortcut(QKeySequence(Qt::AltModifier|Qt::Key_F4));
#else
exit->setShortcuts(QKeySequence::Quit);
#endif
// *
QAction *undo = new QAction("Undo", this);
undo->setIcon(QIcon("../undo.png"));
undo->setStatusTip("Undo the last change");
QAction *redo = new QAction("Redo", this);
redo->setIcon(QIcon("../redo.png"));
redo->setStatusTip("Revert the last undo");
QAction *cut = new QAction("Cut", this);
cut->setIcon(QIcon("../cut.png"));
cut->setToolTip("Tool Tip for Cut");
cut->setStatusTip("Cut selected text");
cut->setWhatsThis("Information about Cut "
"(what's this)");
QAction *copy = new QAction("Copy", this);
copy->setIcon(QIcon("../copy.png"));
copy->setToolTip("Tool Tip for Copy");
copy->setStatusTip("Copy selected text to clipboard");
copy->setWhatsThis("Information about Copy "
"(what's this)");
QAction *paste = new QAction("Paste", this);
paste->setIcon(QIcon("../paste.png"));
paste->setToolTip("Tool Tip for Paste");
paste->setStatusTip("Paste contents of clipboard");
paste->setWhatsThis("Information about Paste "
"(what's this)");
QAction *about = new QAction("About Journal", this);
connect(about, &QAction::triggered,
this, &MainWindow::showAboutJournal);
QMenu *fileMenu = menuBar()->addMenu("File");
fileMenu->addAction(openFile);
fileMenu->addAction(closeFile);
fileMenu->addAction(saveFile);
fileMenu->addSeparator();
fileMenu->addAction(exit);
QMenu *editMenu = menuBar()->addMenu("Edit");
editMenu->addAction(undo);
editMenu->addAction(redo);
editMenu->addSeparator();
editMenu->addAction(cut);
editMenu->addAction(copy);
editMenu->addAction(paste);
QMenu *helpMenu = menuBar()->addMenu("Help");
helpMenu->addAction(QWhatsThis::createAction());
helpMenu->addAction(about);
// *
QToolBar *fileToolBar = addToolBar("Uncheck to turn off "
"tool bar for File Menu");
fileToolBar->addAction(openFile);
fileToolBar->addAction(closeFile);
fileToolBar->addAction(saveFile);
QToolBar *editToolBar = addToolBar("Edit Menu");
editToolBar->addAction(undo);
editToolBar->addAction(redo);
editToolBar->addSeparator();
editToolBar->addAction(cut);
editToolBar->addAction(copy);
editToolBar->addAction(paste);
QToolBar *helpToolBar = addToolBar("Help Menu");
helpToolBar->addAction(QWhatsThis::createAction());
// *
QPushButton *pb_open = new QPushButton();
pb_open->setText("Open");
pb_open->setToolTip("<img src='../file_open.png'> "
"Click to Open File");
pb_open->setWhatsThis("Information about the "
Open Button (what's this)");
QPushButton *pb_close = new QPushButton();
pb_close->setText("Close");
pb_close->setToolTip("Tool Tip for <b>CLOSE</b> "
"Push Button"
"<ul>"
" <li>Item 1</li>"
" <li>Item 2</li>"
" <li>Item 3</li>"
"</ul>");
pb_close->setWhatsThis("Click button to exit program");
// *
statusBar()->showMessage("Temporary Status Message");
QLabel *statusLine = new QLabel(
"Permanent Status Message", this);
statusBar()->addPermanentWidget(statusLine);
QHBoxLayout *layout = new QHBoxLayout();
layout->addStretch();
layout->addWidget(pb_open);
layout->addSpacing(25);
layout->addWidget(pb_close);
layout->addStretch();
centralWidget->setLayout(layout);
connect(pb_open, &QPushButton::clicked, openFile,
&QAction::triggered);
connect(pb_close, &QPushButton::clicked, exit,
&QAction::triggered);
}
The source to set up the actions, menu, toolbar, tool tips, and “What’s This” help were presented in the preceding four examples. In this example we are going to add a status bar to display various temporary and permanent messages.
There is a method in the QAction class called setStatusTip() which is passed a QString parameter. This method will store the string as part of the action. When the user hovers the mouse over any item associated with the action, the stored string will be displayed as a temporary message on the left side of the main window status bar. Lines 35, 39, 44, 51, and 58 set up a status tip for undo, redo, cut, copy, and paste. In our application the status bar will display the appropriate message when the user hovers over a menu item or a tool bar item. This message will disappear when the mouse is moved off the action.
Line 125 shows how to add a temporary message to the status bar. This message is shown when the program starts and then disappears when the mouse is moved to some other action.
There is another kind of status bar message called a permanent message. These are shown on the far right side of the main window and are visible for the life time of the window. A permanent message must be located inside a widget and then the widget is added to the status bar. The most common widget is a QLabel. On line 127 a new label is created and then on line 129 the label is placed on the status bar by calling addPermanentWidget().
Signal / Slot Connections
The connect() on line 12 invokes the selectFile() method when the user clicks on the Open push button. This was described in example 28.
The connect() on line 63 invokes the showAboutJournal() method which will display the “About Journal” message. This was described in example 26.
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_30”.