Qt menu bar, toolbar and status bar

Qt abstracts the elements that the user interacts with the interface into an "action", represented by the QAction class. QAction can be added to menus and toolbars. During the period, we also introduced some details in detail, such as the use of resource files, object model and layout manager. This section introduces in detail about the menu bar, toolbar and status bar.

We assume that the window is still built on top of the QMainWindow class, which will make our development much easier. Of course, in the actual development process, QMainWindow is usually only used as the "main window", and the dialog window uses the QDialog class more. As we will see later, the QDialog class lacks some convenient functions provided by the QMainWindow class, such as menuBar() and toolBar().

openAction = new QAction(QIcon(":/images/open_Icon"), tr("&Open..."), this);
openAction->setShortcuts(QKeySequence::Open);
openAction->setStatusTip(tr("Open an existing file"));
connect(openAction, &QAction::triggered, this, &MainWindow::open);

QMenu *file = menuBar()->addMenu(tr("&File"));
file->addAction(openAction);

QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);

We see that Qt creates a menu bar for us using the menuBar() function. menuBar() is a function provided by QMainWindow, so you won't find it in QWidget or QDialog. This function returns the window's menu bar, or creates a new one if there is no menu bar. This also explains why we can directly use the return value of the menuBar() function, after all, we have not created a menu bar object! Turns out, this is what menuBar() created and returned for us.

In Qt, the class that represents the menu is QMenuBar (you should have thought of this name). QMenuBar represents a menu bar at the top of the window. We add a menu to it using its addMenu() function. Although we only provided a string as an argument, Qt will not display it as the text of the newly created menu. As for the ampersand, we have already explained that this creates a shortcut key for the menu. When we create a menu object, we can add QAction to the menu, which is the role of the addAction() function.

The QToolBar section below is very similar. As the name suggests, QToolBar is a toolbar. We use the addToolBar() function to add new toolbars. Why is the previous one menuBar() and now addToolBar()? Because a window has only one menu bar, but it may have multiple toolbars. If we modify the code a bit:

QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);

QToolBar *toolBar2 = addToolBar(tr("Tool Bar 2"));
toolBar2->addAction(openAction);

We see that there are now two toolbars:

The toolbar can be set as fixed, floating, etc. For specific settings, please refer to the Qt documentation.

As we said before, use QAction::setStatusTip() to set the prompt text of the action on the status bar. But when we put the mouse on the button now, we cannot see the prompt text. The reason is simple, we didn't add a status bar. How to add it? Similar to the previous QMainWindow::menuBar(), QMainWindow has a statusBar() function. Let's add this function:

statusBar();

We added a solitary statusBar() that seems nondescript, but, similar to the previous implementation of menuBar(), this function will return a QStatusBar object, if not, create it first and then return it.

QStatusBar inherits QWidget, so we can add any other QWidget subclasses to the status bar to realize a complex status bar with proportional display and grid switch similar to the bottom of the Photoshop window. For more information on QStatusBar, refer to the Qt documentation.

What about QDialog or QWidget without these functions? Remember, both QToolBar and QStatusBar are subclasses of QWidget, so we can add it to another QWidget with a layout manager. The QLayout layout provides the setMenuBar() function, which can easily add a menu bar. See the documentation for details.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_60259116/article/details/128791641