当前位置:网站首页>QT学习日记7——QMainWindow
QT学习日记7——QMainWindow
2022-07-02 07:23:00 【herb.dr】
目录
一、QMainWindow简介
1.1 文字
QMainWindow 是一个为用户提供主窗口程序的类,包含一个菜单栏(menu bar)、多个工具栏(tool bars)、多个锚接部件(dock widgets)、一个状态栏(status bar)及一个中心部件(central widget),是许多应用程序的基础,如文本编辑器,图片编辑器等
1.2 图示

二、创建界面
2.1 选择QMainWindow


2.2 创建菜单栏
1、创建一个菜单栏没有扔到屏幕上

2、把菜单栏放入窗口,并创建菜单
菜单栏最多只能有一个

#include "mainwindow.h"
#include <QMenuBar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 重置窗口大小
resize(600, 400);
// 菜单栏创建
QMenuBar *bar = menuBar();
// 将菜单栏放入到窗口中
setMenuBar(bar);
// 创建菜单
QMenu* fileMenu = bar->addMenu("文件");
QMenu* editMenu = bar->addMenu("编辑");
}
MainWindow::~MainWindow()
{
}3、创建菜单项

4、添加分隔符

2.3 创建工具栏
1、默认生成
#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 重置窗口大小
resize(600, 400);
// 菜单栏创建
QMenuBar *bar = menuBar();
// 将菜单栏放入到窗口中
setMenuBar(bar);
// 创建菜单
QMenu* fileMenu = bar->addMenu("文件");
QMenu* editMenu = bar->addMenu("编辑");
fileMenu->addAction("新建");
fileMenu->addSeparator();
fileMenu->addAction("打开");
QToolBar* toolBar = new QToolBar(this);
addToolBar(toolBar);
}
MainWindow::~MainWindow()
{
}
2、在左边生成
可以查文档有关这个函数其他参数

3、只允许左右停靠

4、不允许浮动

5、移动的总开关
关闭这个开关,上面设置的东西都没有意义了
toolBar->setMovable(false);
6、在工具栏中设置内容

#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 重置窗口大小
resize(600, 400);
// 菜单栏创建
QMenuBar *bar = menuBar();
// 将菜单栏放入到窗口中
setMenuBar(bar);
// 创建菜单
QMenu* fileMenu = bar->addMenu("文件");
QMenu* editMenu = bar->addMenu("编辑");
QAction* newAction = fileMenu->addAction("新建");
fileMenu->addSeparator();
QAction* openAction = fileMenu->addAction("打开");
QToolBar* toolBar = new QToolBar(this);
addToolBar(Qt::LeftToolBarArea, toolBar);
// 只允许 左右停靠
toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea );
toolBar->setFloatable(false);
toolBar->addAction(newAction);
toolBar->addAction(openAction);
}
MainWindow::~MainWindow()
{
}
添加分隔线

7、工具栏中添加控件

2.4 状态栏
1、状态栏
最多只能有一个
#include "mainwindow.h"
#include <QMenuBar>
#include <QToolBar>
#include <QDebug>
#include <QPushButton>
#include <QStatusBar>
#include <QLabel>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 重置窗口大小
resize(600, 400);
// 菜单栏创建
QMenuBar *bar = menuBar();
// 将菜单栏放入到窗口中
setMenuBar(bar);
// 创建菜单
QMenu* fileMenu = bar->addMenu("文件");
QMenu* editMenu = bar->addMenu("编辑");
QAction* newAction = fileMenu->addAction("新建");
fileMenu->addSeparator();
QAction* openAction = fileMenu->addAction("打开");
QToolBar* toolBar = new QToolBar(this);
addToolBar(Qt::LeftToolBarArea, toolBar);
// 只允许 左右停靠
toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea );
toolBar->setFloatable(false);
toolBar->addAction(newAction);
toolBar->addSeparator();
toolBar->addAction(openAction);
QPushButton* btn = new QPushButton("aa", this);
toolBar->addWidget(btn);
// 状态栏 最多有一个
QStatusBar* stBar = statusBar();
// 设置到窗口中
setStatusBar(stBar);
// 创建标签控件
QLabel* label = new QLabel("提示信息", this);
// 把标签控件放进状态栏
stBar->addWidget(label);
}

2、右侧提示信息
2.5 铆接部件
加头文件 #include <QDockWidget>

设置后期停靠区域,只允许上下

2.6 设置中心部件
只能设置一个
添加头文件 #include <QTextEdit>

边栏推荐
- Retrofit's callback hell is really vulnerable in kotlin synergy mode!
- Use WinDbg to statically analyze dump files (summary of practical experience)
- 618再次霸榜的秘密何在?耐克最新财报给出答案
- 14. Code implementation of semaphore
- 【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
- 2022-06-17
- 洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
- Convert yv12 to rgb565 image conversion, with YUV to RGB test
- 6种单例模式的实现方式
- MySQL lethal serial question 4 -- are you familiar with MySQL logs?
猜你喜欢
随机推荐
shell编程01_Shell基础
快应用中实现自定义抽屉组件
华为应用市场应用统计数据问题大揭秘
Leetcode+ 76 - 80 storm search topic
JS settimeout() and interview questions
In the face of uncertainty, the role of supply chain
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
js数组常用方法
软件产品管理系统有哪些?12个最佳产品管理工具盘点
PCL 从一个点云中提取一个子集
14.信号量的代码实现
Set the playback speed during the playback of UOB equipment
从MediaRecord录像中读取H264参数
Mysql database remote access permission settings
【TS】1368- 秒懂 TypeScript 泛型工具类型!
JSP webshell免殺——JSP的基礎
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
Solution of mysql8 forgetting password file in Windows Environment
Use WinDbg to statically analyze dump files (summary of practical experience)
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)








