当前位置:网站首页>QMainWindow 详解
QMainWindow 详解
2022-07-29 09:07:00 【比特冬哥】
QMainWindow是什么?
QMainWindow是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar)、多个工具栏(tool bars)、多个锚接部件(dock widgets)、一个状态栏(status bar)及一个中心部件(central widget),是许多应用程序的基础,如文本编辑器,图片编辑器等。
1. 菜单栏
一个主窗口最多只有一个菜单栏。位于主窗口顶部、主窗口标题栏下面。
- 创建菜单栏,通过QMainWindow类的menubar()函数获取主窗口菜单栏指针
QMenuBar * menuBar() const
- 创建菜单,调用QMenu的成员函数addMenu来添加菜单
QAction* addMenu(QMenu * menu)
QMenu* addMenu(const QString & title)
QMenu* addMenu(const QIcon & icon, const QString & title)
- 创建菜单项,调用QMenu的成员函数addAction来添加菜单项
QAction* activeAction() const
QAction* addAction(const QString & text)
QAction* addAction(const QIcon & icon, const QString & text)
QAction* addAction(const QString & text, const QObject * receiver,const char * member, const QKeySequence & shortcut = 0)
QAction* addAction(const QIcon & icon, const QString & text, const QObject * receiver, const char * member, const QKeySequence & shortcut = 0)
Qt 并没有专门的菜单项类,只是使用一个QAction类,抽象出公共的动作。当我们把QAction对象添加到菜单,就显示成一个菜单项,添加到工具栏,就显示成一个工具按钮。用户可以通过点击菜单项、点击工具栏按钮、点击快捷键来激活这个动作。
2. 工具栏
主窗口的工具栏上可以有多个工具条,通常采用一个菜单对应一个工具条的的方式,也可根据需要进行工具条的划分。
直接调用QMainWindow类的addToolBar()函数获取主窗口的工具条对象,每增加一个工具条都需要调用一次该函数。
插入属于工具条的动作,即在工具条上添加操作。通过QToolBar类的addAction函数添加。
工具条是一个可移动的窗口,它的停靠区域由QToolBar的allowAreas决定,包括:
Qt::LeftToolBarArea //停靠在左侧
Qt::RightToolBarArea //停靠在右侧
Qt::TopToolBarArea //停靠在顶部
Qt::BottomToolBarArea //停靠在底部
Qt::AllToolBarAreas //以上四个位置都可停靠
使用setAllowedAreas()函数指定停靠区域:
setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea)
使用setMoveable()函数设定工具栏的可移动性:
setMoveable(false)//工具条不可移动, 只能停靠在初始化的位置上
3. 状态栏
派生自QWidget类,使用方法与QWidget类似,QStatusBar类常用成员函数:
状态栏也只能最多有一个
//添加小部件
void addWidget(QWidget * widget, int stretch = 0)
//插入小部件
int insertWidget(int index, QWidget * widget, int stretch = 0)
//删除小部件
void removeWidget(QWidget * widget)
4. 铆接部件
铆接部件 QDockWidget,也称浮动窗口,可以有多个。
QDockWidget * dock = new QDockWidget("标题",this);
addDockWidget(Qt::LeftDockWidgetArea,dock);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea | Qt::TopDockWidgetArea); //设置区域范围
5. 核心部件(中心部件)
除了以上几个部件,中心显示的部件都可以作为核心部件,例如一个记事本文件,可以利用QTextEdit做核心部件
QTextEdit * edit = new QTextEdit(this);
setCentralWidget(edit);
边栏推荐
- BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
- Quaternion and its simple application in unity
- Curl -v | JQ
- How to quickly experience oneos
- State compression DP
- Redis series 3: highly available master-slave architecture
- C language -- 22 one dimensional array
- Compile and install Apache for rocky Foundation
- [unity entry program] C # and unity - understand classes and objects
- Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
猜你喜欢

2022电工(初级)考题模拟考试平台操作

How does alternates achieve high-performance publish and subscribe?

How does xjson implement four operations?

A structured random inactivation UNET for retinal vascular segmentation
网络原理笔记(五层网络)

Common query optimization technology of data Lake - "deepnova developer community"

Flowable 基础篇1

Flowable 高级篇

Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)

Tesseract图文识别--简单
随机推荐
A structured random inactivation UNET for retinal vascular segmentation
Floweable advanced
[LOJ 6485] LJJ binomial theorem (unit root inversion) (template)
Memory leaks and common solutions
Flowable 高级篇
用户身份标识与账号体系实践
Redis series 3: highly available master-slave architecture
【机器学习】朴素贝叶斯代码练习
User identity identification and account system practice
LeetCode刷题(6)
Classic interview question: = = the difference between equals
2022年R2移动式压力容器充装考题模拟考试平台操作
Leetcode question brushing (6)
[unity entry program] collection of common learning websites
Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network
Flowable UI制作流程图
工业测控设备内生信息安全技术研究综述
Jetpack Glance? The spring of widgets is coming
md
Sword finger offer 50. the first character that appears only once