A tool tip provides a hint or relevant additional information about the current object. In this example tool tips are added for several actions and the push buttons located on the central window. The hint will be displayed when the user hovers the mouse over the corresponding toolbar item or push button.
#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 method declarations.
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"));
QAction *redo = new QAction("Redo", this);
redo->setIcon(QIcon("../redo.png"));
QAction *cut = new QAction("Cut", this);
cut->setIcon(QIcon("../cut.png"));
cut->setToolTip("Tool Tip for Cut");
QAction *copy = new QAction("Copy", this);
copy->setIcon(QIcon("../copy.png"));
copy->setToolTip("Tool Tip for Copy");
QAction *paste = new QAction("Paste", this);
paste->setIcon(QIcon("../paste.png"));
paste->setToolTip("Tool Tip for Paste");
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_open = new QPushButton();
pb_open->setText("Open");
pb_open->setToolTip("<img src='../file_open.png'> "
"Click to Open File");
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>");
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 code to set up the actions, menu, and toolbar were presented in the prior two examples. In this example tool tips will be introduced and a second push button associated with the Open menu item is added to the central window.
The calls to setToolTip() on lines 40, 44, and 48 define the hint or detailed message displayed to the user when they hover the mouse over the Cut, Copy, or Paste icons located on the toolbar.
We can also add tool tips to the two push buttons located in the central widget of the application. On line 90 a tool tip for the Open push button is set. This tool tip includes the file open image for a better visual experience. On line 96 the tool tip for the Close push button shows more text than the other hints and it also uses a few standard HTML tags. This tool tip is not a context menu since you can not select any of the items.
On line 113 is a call to connect() which is slightly different from other connections we have shown. The first and third parameters are the Sender and Receiver objects. Typically we pass this for the third parameter which is the MainWindow object. On this line we are passing openFile which is a QAction. The “slot” in this code is actually a Signal which means we are connecting a Signal to a Signal. This will have the effect of emitting a QAction signal when the user clicks the QPushButton named Open. This will cause the connect() on line 12 to be activated.
For consistency the call to connect() on line 116 has been modified to also connect a Signal to a Signal. When the user clicks the Close button QAction will emit a Signal and the connect on line 23 will be triggered.
Signal / Slot Connections
In the code shown above the call to connect() on line 12 sets up which slot method is invoked when “File”, “Open” is selected on the menu or the Open icon is selected on the toolbar is selected. Since the openFile action is used for both items we only need to make one connection. Selecting either one will invoke the selectFile() method which is shown below.
This same connection on line 12 will also be activated when the user clicks the Open push button on the central window and the connect() from line 113 triggers the QAction signal.
void MainWindow::selectFile()
{
QMessageBox::about(this, "Open File",
"User selected the Open File action");
}
The call to connect() on line 51 will invoke the showAboutJournal() method and display the “About Journal” message box. 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_28”.