Get the App
SLTechnology News&Howtos  ›  Servers  › 

Subclassing QMainWindows, adding menu bars and toolbars, and adding icons

Shulou Source: shulou.com Published: 2022-06-02 11:10:15 10月23日 Update

Mymainwindow.cpp

# include "mymainwindow.h" # include "ui_mymainwindow.h" / / # include / / uses forward declaration instead of header file inclusion, mainly to compile the forward declaration myMainWindow::myMainWindow (QWidget * parent) of faster class QMenuBar;// classes: QMainWindow (parent), ui (new Ui::myMainWindow) {ui- > setupUi (this); openAction = new QAction (tr ("& open"), this) OpenAction- > setShortcut (QKeySequence::Open); / / Shortcut keys for actions For example: open shortcut Ctrl+O (defined by Qt's QKeySequence) openAction- > setStatusTip (tr ("open a file")); / / prompt statement for adding status bar file = menuBar ()-> addMenu (tr ("& File")); file- > addAction (openAction); toolBar = addToolBar (tr ("& File")); toolBar- > addAction (openAction);} myMainWindow::~myMainWindow () {delete ui;}

After running, as shown in the figure:

Add icons: 1. First, use the resource file of Qt. Right-click on the QtCreator project, select New File..., and then select resource file.

2. Then click choose, name it, select a good location, and then finish the next step. For ease of use, I will build this file in the root directory. I suggest that after carefully planning the file, it should be built in a special rsources folder. When you're done, a .qrc file is generated, and qrc is actually an abbreviation for Qt Recource Collection.

3. Click the Add button, first select Add prefix, and then change the generated / new/prefix to /. This is prefix, which is the prefix you need to provide when using icons later, starting with /. After adding prefix, add an icon in the project file, select Add file, and select that icon. When you are done, you can save the qrc file.

What do you think of the path of the picture? As you can see, Qt's resource file view uses a tree structure, the root is /, the leaf node is the image location, and connected together is the path. For example, the path to this image is / resource/Open.png.

4. Add code

Forward declaration of the class QIcon; / / class. MyMainWindow::myMainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::myMainWindow) {ui- > setupUi (this); openAction = new QAction (tr ("& open"), this); openAction- > setShortcut (QKeySequence::Open); / / shortcut keys for actions For example: open shortcut Ctrl+O (defined by Qt's QKeySequence) openAction- > setStatusTip ("open a file"); / / add the prompt statement openAction- > setIcon (QIcon (": / resource/open.png")) in the status bar; / / add this code and add the icon file = menuBar ()-> addMenu (tr ("& File")); file- > addAction (openAction); toolBar = addToolBar ("& File"); toolBar- > addAction (openAction) }

Run as shown in the figure:

Tags: Files selections icons that is keyboard shortcuts pictures paths code locations actions status statements resources such as figure hint generation run easy to use prefix leaf Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology NVidia vpn Docker