第二十七课、应用程序中的主窗口------------------狄泰软件学院
一、主窗口的概念
1、应用程序中的主窗口
(1)、主窗口是与用户进行长时间交互的顶级窗口
(2)、程序的绝大多数功能直接由主窗口提供
(3)、主窗口通常是应用程序启动后显示的第一个窗口
(4)、整个程序由一个主窗口和多个对话框组成
2、Qt中的主窗口
(1)、Qt开发平台中直接支持主窗口的概念
(2)、QMainWindow是Qt中主窗口的基类
(3)、QMainWindow是继承于QWidget的一种容器类组件
3、QMainWindow中封装的秘密
4、QMainWindow中组件布局
二、主窗口中的菜单
1、在Qt中提供与菜单相关的类组件
2、在主窗口中创建菜单
3、快捷键的设置
思路:三板斧
(1)、菜单栏函数里创建菜单栏对象,并且调用菜单函数(参数为菜单栏对象)
(2)、菜单函数里创建菜单,并且调用菜单项函数(参数为返回菜单项的引用、菜单项的名字、快捷键),添加菜单项到菜单,添加菜单到菜单栏
(3)、菜单项函数里创建菜单项,设置菜单项名字和快捷键(父组件先不用设置)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMenuBar> #include <QMenu> #include <QAction> #include <QString> #include <QtGui/QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT private: MainWindow(QWidget *parent = 0); MainWindow(const MainWindow& obj); MainWindow* operator = (const MainWindow& obj); 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 ket);//菜单项 public: static MainWindow* NewInstance(); ~MainWindow(); }; #endif // MAINWINDOW_H
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "MainWindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { } bool MainWindow::construct() { bool ret = true; ret = ret && initMenuBar(); return ret; } MainWindow* MainWindow::NewInstance() { MainWindow* ret = new MainWindow(); if((ret==NULL) || (!ret->construct())) { delete ret; ret = NULL; } return ret; } bool MainWindow::initMenuBar()//菜单栏 { bool ret = true; QMenuBar* mb = menuBar();//一定要注意是menuBar(),这是普通成员函数,不是构造函数 ret = ret && initFileMenu(mb);//传一个参数是为了在initFileMenu()函数将menu加入菜单栏 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)");//创建文件菜单,(&F)是为了可以Alt+F打开 ret = (menu != NULL); if(ret) { QAction* action = NULL; //New ret = ret && makeAction(action, "New(&N)",Qt::CTRL + Qt::Key_N); if(ret) { menu->addAction(action); } menu->addSeparator(); //Open ret = ret && makeAction(action, "Open(&O)...",Qt::CTRL + Qt::Key_O); if(ret) { menu->addAction(action); } menu->addSeparator(); //Save ret = ret && makeAction(action, "Save(&S)",Qt::CTRL + Qt::Key_S); if(ret) { menu->addAction(action); } menu->addSeparator(); //Save As ret = ret && makeAction(action, "Save As(&A)...",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //print ret = ret && makeAction(action, "Print(&P)...",Qt::CTRL + Qt::Key_P); if(ret) { menu->addAction(action); } menu->addSeparator(); //Exit ret = ret && makeAction(action, "Exit(&X)",0); if(ret) { menu->addAction(action);//将菜单项加入到菜单 } } if(ret) { mb->addMenu(menu);//将菜单加入到菜单栏 } else { delete mb; } return ret; } /************************************************编辑菜单********************************************************/ bool MainWindow::initEditMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Edit(&E)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Undo ret = ret && makeAction(action, "Undo(&U)",Qt::CTRL + Qt::Key_Z); if(ret) { menu->addAction(action); } menu->addSeparator(); //Redo ret = ret && makeAction(action, "Redo(&R)...",Qt::CTRL + Qt::Key_Y); if(ret) { menu->addAction(action); } menu->addSeparator(); //Cut ret = ret && makeAction(action, "Cut(&T)",Qt::CTRL + Qt::Key_X); if(ret) { menu->addAction(action); } menu->addSeparator(); //Copy ret = ret && makeAction(action, "Copy(&C)...",Qt::CTRL + Qt::Key_C); if(ret) { menu->addAction(action); } menu->addSeparator(); //Pase ret = ret && makeAction(action, "Pase(&P)...",Qt::CTRL + Qt::Key_V); if(ret) { menu->addAction(action); } menu->addSeparator(); //Delete ret = ret && makeAction(action, "Delete(&L)",Qt::Key_Delete); if(ret) { menu->addAction(action); } menu->addSeparator(); //Find ret = ret && makeAction(action, "Find(&F)...",Qt::CTRL + Qt::Key_F); if(ret) { menu->addAction(action); } menu->addSeparator(); //Replace ret = ret && makeAction(action, "Replace(&R)...",Qt::CTRL + Qt::Key_H); if(ret) { menu->addAction(action); } menu->addSeparator(); //Goto ret = ret && makeAction(action, "Goto(&G)",Qt::CTRL + Qt::Key_G); if(ret) { menu->addAction(action); } menu->addSeparator(); //Select All ret = ret && makeAction(action, "Select All(&A)",Qt::CTRL + Qt::Key_A); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************格式菜单********************************************************/ bool MainWindow::initFormatMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Format(&O)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Auto Wrap ret = ret && makeAction(action, "Auto Wrap(&W)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Font ret = ret && makeAction(action, "Font(&F)...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************视图菜单********************************************************/ bool MainWindow::initViewMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("View(&V)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //Tool Bar ret = ret && makeAction(action, "Tool Bar(&T)",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //Status Bar ret = ret && makeAction(action, "Status Bar(&S)",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } /************************************************帮助菜单********************************************************/ bool MainWindow::initHelpMenu(QMenuBar* mb) { bool ret = true; QMenu* menu = new QMenu("Help(&H)"); ret = (menu != NULL); if(ret) { QAction* action = NULL; //User Manual ret = ret && makeAction(action, "User Manual",0); if(ret) { menu->addAction(action); } menu->addSeparator(); //About NotePad ret = ret && makeAction(action, "About NotePad...",0); if(ret) { menu->addAction(action); } } if(ret) { mb->addMenu(menu); } else { delete mb; } return ret; } bool MainWindow::makeAction(QAction*& action, QString text, int key)//菜单项 { bool ret = true; action = new QAction(text, NULL); if(action != NULL) { action->setShortcut(QKeySequence(key));//创建快捷键 } else { ret = false; } return ret; } MainWindow::~MainWindow() { }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <QtGui/QApplication> #include "MainWindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow* w = MainWindow::NewInstance(); w->show(); return a.exec(); }
三、小结
(1)、主窗口是与用户进行长时间交互的顶级窗口
(2)、主窗口通常是应用程序启动后显示的第一个窗口
(3)、QMainWindow是Qt中主窗口的基类
(4)、QMainWindow是继承于QWidget的一种容器类组件
(5)、QMainWindow封装了菜单栏、工具栏、状态栏等组件