In this example a tool bar is added to a user application. A tool bar typically contains the most commonly used menu items. The same actions and icons used for the menu will be used on the tool bar.
#include <QtCore>
#include <QtGui>
class MainWindow : public QMainWindow
{
public:
MainWindow();
private:
void showAboutJournal();
};
Our class declaration for MainWindow has a public constructor and one private slot method. This code is identical to example 26.
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"));
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"));
QAction *redo = new QAction("Redo", this);
redo->setIcon(QIcon("../redo.png"));
QAction *cut = new QAction("Cut", this);
cut->setIcon(QIcon("../cut.png"));
QAction *copy = new QAction("Copy", this);
copy->setIcon(QIcon("../copy.png"));
QAction *paste = new QAction("Paste", this);
paste->setIcon(QIcon("../paste.png"));
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(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);
// *
QPushButton *pb_close = new QPushButton();
pb_close->setText("Close");
QHBoxLayout *layout = new QHBoxLayout();
layout->addStretch();
layout->addWidget(pb_close);
layout->addStretch();
centralWidget->setLayout(layout);
connect(pb_close, &QPushButton::clicked,
this, &QWidget::close);
}
The source code prior to line 65 is identical to the code shown in example 26. Please refer to that example for information and a full discussion about the process of adding a menu to a main window.
The code shown on lines 68 through 80 are the only changes from the prior example. Line 68 calls QMainWindow::addToolBar() and returns a pointer to a new QToolBar. The existing actions for Open, Close, and Save are then added to the toolbar. Hovering the mouse over the icons for these actions will show the menu item text. On line 74 a second QToolBar is created and then the edit menu items are added.
To the left of the toolbar is a “handle”. Hovering over this handle will change the cursor from an arrow to what is called a “split V cursor”. If you hold down the left mouse button the toolbar can be dragged vertically or horizontally and repositioned on the main window.
From anywhere on the toolbar you can click the right mouse button and a context menu will be displayed. The text passed when creating the toolbars will be displayed with a check box to the left. If you uncheck either one the corresponding toolbar will disappear.
Signal / Slot Connections
Line 46 in the code shown above contains a call to connect(). This sets up what action should be taken when the user selects the “Help”, “About Journal” menu item. The following slot method is called and displays a message box for the user.
void MainWindow::showAboutJournal()
{
QMessageBox::about(this, "About Journal",
"Text which typically contains the Developer "
"or Organization Name, Version Number, and "
"Copyright details.");
}
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_27”.