The “What’s This” feature can be used to provide additional information about a menu option, push buttons or other widgets. This example adds “What’s This” help and also shows how to add a special question mark icon to a menu item and the tool bar for easy access to this feature.
#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"));
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");
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->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->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(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");
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 and tool tips were presented in the three preceding examples. In this example we are adding a “What’s This” object to a few menu items, tool bar items, and the two push buttons.
Before talking about the implementation we want to discuss how the user can access a the “What’s This” help. There is a question mark icon under the Help menu and also on the tool bar. After you click this icon a circle with a line through it will appear. When you mouse over a menu or tool bar item which has “What’s This” help, the icon will change to an arrow with a question mark to indicate this action has a “What’s This” object. The other way to use this feature is by pressing Shift+F1.
For example, if you click the question mark icon on the far right side of the tool bar and then hover the mouse over the scissors or cut icon, the cursor will change. Now click on the cut icon and a yellow box will be displayed saying “Information about Cut (what’s this)”.
Now let’s look at the new source code. On lines 41, 46, and 51 a “What’s This” message is set for three QAction objects. This will set the “What’s This” help message for both the menu and the tool bar. On line 99 and 113 we call setWhatsThis() for the two push buttons. The help message can be plain text or include a limited set of HTML commands.
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 57 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_29”.