Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report