当前位置:网站首页>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
边栏推荐
- Gslx680 touch screen driver source code analysis (gslx680. C)
- Rust Versus Go(哪种是我的首选语言?)
- Linux server development, MySQL cache strategy
- Leanote private cloud note building
- Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
- [UTCTF2020]file header
- Introduction to basic components of wechat applet
- Téléchargement des données de conception des puces
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- Route jump in wechat applet
猜你喜欢
解决问题:Unable to connect to Redis
Sign up now | oar hacker marathon phase III, waiting for your challenge
mysql多列索引(组合索引)特点和使用场景
Figure out the working principle of gpt3
2022焊工(初级)判断题及在线模拟考试
MySQL multi column index (composite index) features and usage scenarios
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
[SUCTF 2019]Game
json 数据展平pd.json_normalize
Visualization Document Feb 12 16:42
随机推荐
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
Thinkcmf6.0安装教程
leanote私有云笔记搭建
Solve could not find or load the QT platform plugin "xcb" in "
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
Explore Cassandra's decentralized distributed architecture
通信设备商,到底有哪些岗位?
2022焊工(初级)判断题及在线模拟考试
[SUCTF 2019]Game
Ansible
[P2P] local packet capturing
Button wizard script learning - about tmall grabbing red envelopes
C语言队列
[mathematical notes] radian
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
numpy中dot函数使用与解析
Resource create package method
Idea add class annotation template and method template
nacos
leetcode:105. Constructing binary trees from preorder and inorder traversal sequences