当前位置:网站首页>Qt学习27 应用程序中的主窗口
Qt学习27 应用程序中的主窗口
2022-07-07 04:31:00 【一个小黑酱】
Qt学习27 应用程序中的主窗口
主窗口的概念
应用程序中的主窗口
- 主窗口是与用户 进行长时间交互的顶层窗口
- 程序的 绝大多数功能 直接由主窗口提供
- 主窗口通常是 应用程序启动后显示的第一个窗口
- 整个程序由一个主窗口和 多个对话框 组成
Qt 中的主窗口
- Qt 开发平台中直接支持主窗口的概念
QMainWindow是 Qt 中主窗口的基类QMainWindow继承于QWidget是一种 容器类型 的组件

QMainWindow中封装的秘密

QMainWindow中的组件布局

- 在 Qt 中提供与菜单相关的类组件

- 在 Qt 主窗口中创建菜单
QMenuBar* mb = menuBar(); // member function
QMenu* menu = new QMenu("File(&F)");
QAction* action = new QAction("New", NULL);
menu->addAction(action);
mb->addMenu(menu);
编程实验 - 主窗口中创建菜单
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QAction>
#include <QMenu>
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
MainWindow();
bool construct();
bool initMenuBar();
bool initFileMenu(QMenuBar* mb);
bool initEditMenu(QMenuBar* mb);
bool initFormatMenu(QMenuBar* mb);
bool initViewMenu(QMenuBar* mb);
bool initHelpMenu(QMenuBar* mb);
bool makeAction(QAction*& action, QString text, int key=0);
bool myAddAction(QMenu* menu, QString text, int key=0);
public:
static MainWindow* NewInstance();
~MainWindow();
};
#endif // MAINWINDOW_H
#include "MainWindow.h"
#include <QMenuBar>
#include <QMenu>
MainWindow::MainWindow()
{
}
bool MainWindow::construct()
{
bool ret = true;
ret = initMenuBar();
return ret;
}
bool MainWindow::initMenuBar()
{
bool ret = true;
QMenuBar* mb = menuBar();
ret = ret && initFileMenu(mb);
ret = ret && initEditMenu(mb);
ret = ret && initFormatMenu(mb);
ret = ret && initViewMenu(mb);
ret = ret && initHelpMenu(mb);
return ret;
}
bool MainWindow::initFileMenu(QMenuBar* mb)
{
bool ret = true;
QMenu* menu = new QMenu("File(&F)");
ret = (menu != NULL);
if (ret) {
ret = ret && myAddAction(menu, "New(N)", Qt::CTRL + Qt::Key_N);
ret = ret && myAddAction(menu, "New Window(W)", Qt::CTRL + Qt::SHIFT + Qt::Key_N);
ret = ret && myAddAction(menu, "Open(O)", Qt::CTRL + Qt::Key_O);
ret = ret && myAddAction(menu, "Save(S)", Qt::CTRL + Qt::Key_S);
ret = ret && myAddAction(menu, "Save as(S)", Qt::CTRL + Qt::SHIFT + Qt::Key_S);
menu->addSeparator();
ret = ret && myAddAction(menu, "Page setting(U)");
ret = ret && myAddAction(menu, "Print(P)", Qt::CTRL + Qt::Key_P);
menu->addSeparator();
ret = ret && myAddAction(menu, "Exit(X)", Qt::CTRL + Qt::Key_X);
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initEditMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("Edit(&E)");
ret = (menu != NULL);
if (ret) {
ret = ret && myAddAction(menu, "Undo(U)", Qt::CTRL + Qt::Key_Z);
ret = ret && myAddAction(menu, "Clip(T)", Qt::CTRL + Qt::Key_X);
ret = ret && myAddAction(menu, "Copy(C)", Qt::CTRL + Qt::Key_C);
ret = ret && myAddAction(menu, "Stick(C)", Qt::CTRL + Qt::Key_V);
ret = ret && myAddAction(menu, "Delete(L)", Qt::Key_Delete);
menu->addSeparator();
ret = ret && myAddAction(menu, "Use Bing search", Qt::CTRL + Qt::Key_E);
ret = ret && myAddAction(menu, "Find(F)", Qt::CTRL + Qt::Key_F);
ret = ret && myAddAction(menu, "Find next(N)", Qt::Key_F3);
ret = ret && myAddAction(menu, "Find pre(V)", Qt::SHIFT + Qt::Key_F3);
ret = ret && myAddAction(menu, "Replace(R)", Qt::CTRL + Qt::Key_H);
ret = ret && myAddAction(menu, "Replace(R)", Qt::CTRL + Qt::Key_H);
ret = ret && myAddAction(menu, "Jump(G)", Qt::CTRL + Qt::Key_G);
menu->addSeparator();
ret = ret && myAddAction(menu, "Select all(A)", Qt::CTRL + Qt::Key_A);
ret = ret && myAddAction(menu, "Data(D)", Qt::Key_F5);
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initFormatMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("Format(&O)");
ret = (menu != NULL);
if (ret) {
ret = ret && myAddAction(menu, "Auto wrap(W)");
ret = ret && myAddAction(menu, "Font(F)");
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initViewMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("View(&V)");
ret = (menu != NULL);
if (ret) {
ret = ret && myAddAction(menu, "Zoom(Z)");
ret = ret && myAddAction(menu, "Status bar(S)");
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::initHelpMenu(QMenuBar *mb)
{
bool ret = true;
QMenu* menu = new QMenu("Help(&H)");
ret = (menu != NULL);
if (ret) {
ret = ret && myAddAction(menu, "View Help(H)");
ret = ret && myAddAction(menu, "Send feedback(F)");
ret = ret && myAddAction(menu, "About App(A)");
}
if (ret) {
mb->addMenu(menu);
}
else {
delete menu;
}
return ret;
}
bool MainWindow::makeAction(QAction *&action, QString text, int key)
{
bool ret = true;
action = new QAction(text, NULL);
if (action != NULL) {
if (key != 0) {
action->setShortcut(QKeySequence(key));
}
}
else {
ret = false;
}
return ret;
}
bool MainWindow::myAddAction(QMenu *menu, QString text, int key)
{
bool ret = true;
QAction* action = NULL;
ret = makeAction(action, text, key);
if (ret) {
menu->addAction(action);
}
return ret;
}
MainWindow *MainWindow::NewInstance()
{
MainWindow* ret = new MainWindow();
if ((ret == NULL) || !ret->construct()) {
delete ret;
ret = NULL;
}
return ret;
}
MainWindow::~MainWindow()
{
}
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow* w = MainWindow::NewInstance();
int ret = -1;
if (w != NULL) {
w->show();
ret = a.exec();
}
return ret;
}
小技巧
快捷键设置
action->setShort(QKeySequence(KEY))QKeySequence- Qt 中与 快捷键 相关的类KEY- Qt 中代表 键值 的常量
小结
- 主窗口是与用户 进行长时间交互的顶层窗口
- 主窗口通常是 应用程序启动后显示的第一个窗口
QMainWindow是 Qt 中主窗口的基类QMainWindow是一种 容器类型 的窗口组件QMainWindow中封装了菜单栏,工具栏,状态栏等组件
边栏推荐
- [UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
- [SUCTF 2019]Game
- Leetcode-543. Diameter of Binary Tree
- @component(““)
- [webrtc] M98 screen and window acquisition
- 解决:Could NOT find KF5 (missing: CoreAddons DBusAddons DocTools XmlGui)
- 解决问题:Unable to connect to Redis
- The metauniverse of the platofarm farm continues to expand, with Dao governance as the core
- 大视频文件的缓冲播放原理以及实现
- CentOS7下安装PostgreSQL11数据库
猜你喜欢

1141_ SiCp learning notes_ Functions abstracted as black boxes

2022制冷与空调设备运行操作复训题库及答案

Figure out the working principle of gpt3

Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?

JSON introduction and JS parsing JSON

The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)

Write CPU yourself -- Chapter 9 -- learning notes

nacos
![[2022 ACTF]web题目复现](/img/e4/ab9a1771489d751ee73a79f151d374.png)
[2022 ACTF]web题目复现

科技云报道:从Robot到Cobot,人机共融正在开创一个时代
随机推荐
Redis technology leak detection and filling (II) - expired deletion strategy
Weibo publishing cases
Make a bat file for cleaning system garbage
C语言通信行程卡后台系统
Rust Versus Go(哪种是我的首选语言?)
pytest+allure+jenkins环境--填坑完毕
Most elements
pytorch 参数初始化
Leanote private cloud note building
自定义类加载器加载网络Class
Sign up now | oar hacker marathon phase III, waiting for your challenge
开源生态|打造活力开源社区,共建开源新生态!
gatk4中的interval是什么??
智联+影音,AITO问界M7想干翻的不止理想One
Figure out the working principle of gpt3
Idea add class annotation template and method template
Determining the full type of a variable
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
What is the difference between TCP and UDP?