当前位置:网站首页>Main window in QT learning 27 application
Main window in QT learning 27 application
2022-07-07 07:59:00 【A little black sauce】
Qt Study 27 The main window in the application
The concept of the main window
The main window in the application
- The main window is with the user Top level window for long-term interaction
- programmatic Most functions Directly provided by the main window
- The main window is usually The first window displayed after the application starts
- The whole program consists of a main window and Multiple dialog boxes form
Qt The main window in
- Qt The concept of main window is directly supported in the development platform
QMainWindow
yes Qt The base class of the main window inQMainWindow
Inherited fromQWidget
It's a kind of Container type The components of
QMainWindow
The secret encapsulated in
QMainWindow
Component layout in
- stay Qt Provides menu related class components
- stay Qt Create a menu in the main window
QMenuBar* mb = menuBar(); // member function
QMenu* menu = new QMenu("File(&F)");
QAction* action = new QAction("New", NULL);
menu->addAction(action);
mb->addMenu(menu);
Programming experiment - Create a menu in the main window
#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;
}
Tips
Shortcut key settings
action->setShort(QKeySequence(KEY))
QKeySequence
- Qt China and Shortcut key Related to the classKEY
- Qt Chinese stands for Key value The constant
Summary
- The main window is with the user Top level window for long-term interaction
- The main window is usually The first window displayed after the application starts
QMainWindow
yes Qt The base class of the main window inQMainWindow
It's a kind of Container type Window componentsQMainWindow
The menu bar is encapsulated in , The toolbar , Status bar and other components
边栏推荐
- Ansible
- [VHDL parallel statement execution]
- MySQL multi column index (composite index) features and usage scenarios
- Linux server development, detailed explanation of redis related commands and their principles
- 2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
- [Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
- [GUET-CTF2019]虚假的压缩包
- Solution: could not find kf5 (missing: coreaddons dbusaddons doctools xmlgui)
- CentOS7下安装PostgreSQL11数据库
- LeetCode 90:子集 II
猜你喜欢
通信设备商,到底有哪些岗位?
Custom class loader loads network class
探索Cassandra的去中心化分布式架构
Linux server development, MySQL transaction principle analysis
Most elements
A bit of knowledge - about Apple Certified MFI
有 Docker 谁还在自己本地安装 Mysql ?
[UTCTF2020]file header
[2022 actf] Web Topic recurrence
Leetcode 40: combined sum II
随机推荐
2022年茶艺师(中级)考试试题及模拟考试
Qt学习26 布局管理综合实例
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
2022 tea master (intermediate) examination questions and mock examination
探索干货篇!Apifox 建设思路
Figure out the working principle of gpt3
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
通信设备商,到底有哪些岗位?
Li Kou interview question 04.01 Path between nodes
开源生态|打造活力开源社区,共建开源新生态!
[SUCTF 2019]Game
Jenkins remote build project timeout problem
[CV] Wu Enda machine learning course notes | Chapter 8
大视频文件的缓冲播放原理以及实现
Operation suggestions for today's spot Silver
You Li takes you to talk about C language 6 (common keywords)
Cnopendata American Golden Globe Award winning data
【VHDL 并行语句执行】
PHP exports millions of data