当前位置:网站首页>QT menu bar, toolbar and status bar
QT menu bar, toolbar and status bar
2022-07-27 05:02:00 【PHP code】
Prior to 《 Add action 》 In the article , We already know ,Qt Abstract the elements of user interface interaction into a “ action ”, Use QAction Class represents .QAction Can be added to the menu 、 On the toolbar . period , We also introduced some details , For example, the use of resource files 、 Object model and layout manager . This section is about the menu bar 、 Toolbar and status bar .
Let's assume that the window is still built on QMainWindow Class , This will make our development much simpler . Of course , In the actual development process ,QMainWindow Usually only as “ main window ”, Dialog windows are used more QDialog class . We'll see in the back ,QDialog Classes will be missing QMainWindow Class provides convenient functions , such as menuBar() as well as toolBar().
Let's go back to 《 Add action 》 Code snippet in article :
openAction = new QAction(QIcon(":/images/doc-open"), 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 , Use menuBar() function ,Qt Created a menu bar for us .menuBar() yes QMainWindow Provided function , So you won't be QWidget perhaps QDialog It's found in . This function will return to the menu bar of the window , If there is no menu bar, a new one will be created . That explains , Why can we directly use menuBar() The return value of the function , After all, we didn't create a menu bar object ! original , This is it. menuBar() Created and returned for us .
Qt in , The class representing the menu is QMenuBar( You should have thought of the name ).QMenuBar It represents the menu bar at the top of the window . We use it addMenu() Function to add a menu . Although we only provide a string as a parameter , however Qt To display it as the text of the newly created menu . as for & Symbol , We have explained , This can create a shortcut key for the menu . When we create the menu object , You can put the QAction Add to this menu , That is to say addAction() Function function .
Below QToolBar Part is very similar . seeing the name of a thing one thinks of its function ,QToolBar Toolbar . We use addToolBar() Function to add a new toolbar . Why is the first one menuBar() And now it's addToolBar() Well ? Because a window has only one menu bar , But there may be multiple toolbars . If we change the code :
QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);
QToolBar *toolBar2 = addToolBar(tr("Tool Bar 2"));
toolBar2->addAction(openAction);
We see , Now there are two toolbars :

Toolbars can be set to fixed 、 Floating, etc , For specific settings, please refer to Qt file .
We said earlier , Use QAction::setStatusTip() You can set the prompt text of this action on the status bar . But now we put the mouse on the button , You can't see the prompt text . The reason is simple , We didn't add a status bar . How to add ? Similar to the previous QMainWindow::menuBar(),QMainWindow There is one statusBar() function . Let's add this function :
QToolBar *toolBar2 = addToolBar(tr("Tool Bar 2"));
toolBar2->addAction(openAction);
statusBar();
Then compile and run :

We added a lonely statuBar() It's quite different , however , Same as the one in front menuBar() Similar implementation of , This function returns a QStatusBar object , If not, create it first and then return .
QStatusBar Inherited QWidget, therefore , We can put any other QWidget Add subclasses to the status bar , To achieve something similar Photoshop There is a proportional display at the bottom of the window 、 Complex status bar with grid switches . of QStatusBar For more information , Please refer to Qt file .
For those without these functions QDialog perhaps QWidget How to do it? ? Remember to ,QToolBar as well as QStatusBar All are QWidget Subclasses of , So we can add it in combination with the layout manager to another QWidget above .QLayout The layout provides setMenuBar() function , You can easily add a menu bar . See the document for details .
thus , We have already introduced the elements that make up the window . Combine these elements with layout management , We should be able to implement a simple general window . When we finish the window layout , We can consider adding functions to it . This is the content of our later chapters .
边栏推荐
- 关于gorm的BeforeDelete钩子方法不生效的问题
- Summary of fire safety training materials
- Three paradigms, constraints, some keyword differences,
- Replication of df-gan experiment -- detailed steps of replication of dfgan and forwarding from remote port to local port using mobaxtem
- How do I reset Photoshop preferences? PS method of resetting preferences
- Approval of meeting OA
- MySQL download and installation & perfect uninstall
- Easily download data in power Bi reports with power auto
- Basic configuration of static routing to achieve network wide accessibility
- Transaction database and its four characteristics, principle, isolation level, dirty read, unreal read, non repeatable read?
猜你喜欢

文件对话框

小程序项目如何创建

MySQL storage engine and its differences

ps怎么导入lut预设?Photoshop导入lut调色预设教程

What about PS too laggy? A few steps to help you solve the problem

Dynamic routing configuration

C language address book management system (linked list, segmented storage of mobile phone numbers, TXT file access, complete source code)

【搜索】—— 多源BFS + 最小步数模型

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

Easily download data in power Bi reports with power auto
随机推荐
如何做数据平滑迁移:双写方案
kali系统arp介绍(断网嗅探密码抓包)
自定义视口高度,多余的部分使用滚动
ps样式如何导入?Photoshop样式导入教程
【C语言】自定义类型详解(结构体+枚举+联合)
利用Power Automate,轻松下载Power BI报告中的数据
Detailed explanation of mvcc and its principle
There is no need to install CUDA and cudnn manually. You can install tensorflow GPU through a one-line program. Take tensorflow gpu2.0.0, cuda10.0, cudnn7.6.5 as examples
Jmeter 界面如何汉化?
Plane conversion (displacement, rotation, scaling)
js小技巧
TCP's three handshakes and four waves
「Photoshop2021入门教程」对齐与分布制作波点图案
CDH cluster integration external Flink (improved version - keep pace with the times)
Hiding skills of Photoshop clipping tool
Counting Nodes in a Binary Search Tree
CDH集群集成外部Flink(改进版-与时俱进)
Find a specific number in an ordered array
Final Cut Pro中文教程 (1) 基础认识Final Cut Pro
MySQL download and installation & perfect uninstall