关键词:应用程序中的主窗口、菜单栏的实现
1. 应用程序中的主窗口
- 主窗口是与用户进行长时间交互的顶层窗口
- 程序的绝大多数功能直接由主窗口提供
- 主窗口通常是应用程序启动后显示的第一个窗口
- 整个程序由一个主窗口和多个对话框组成
2. Qt中的主窗口
- Qt开发平台中直接支持主窗口的概念
-
QMainWindow
是Qt中主窗口的基类 -
QMainWindow
继承于QWidget
是一种容器类型的组件
3. QMainWindow
中封装的秘密
4. QMainWindow
中的组件布局
5. 在Qt中提供与菜单相关的类组件
6. 在Qt主窗口中创建菜单
7. 快捷键的设置
8. 主窗口中创建菜单栏
MainWindows.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QKeySequence>
#include <QMenuBar>
#include <QAction>
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
MainWindow();
MainWindow(const MainWindow&);
MainWindow& operator =(const MainWindow&);
bool construct();
bool initMenuBar();
bool initFileMenu(QMenuBar* mb);
bool initEidtMenu(QMenuBar* mb);
bool initFormatMenu(QMenuBar* mb);
bool initViewMenu(QMenuBar* mb);
bool initHelpMenu(QMenuBar* mb);
bool makeAciton(QAction*& action, QString text, int key);
public:
static MainWindow* NewInstance();
~MainWindow();
};
#endif // MAINWINDOW_H
MainWindows.cpp
#include "MainWindow.h"
#include <QMenu>
MainWindow::MainWindow()
{
}
MainWindow* MainWindow::NewInstance()
{
MainWindow* ret = new MainWindow();
if( ret == NULL || !ret->construct())
{
delete ret;
ret = NULL;
}
return ret;
}
bool MainWindow::construct()
{
bool ret = true;
ret = ret && initMenuBar();
return ret;
}
bool MainWindow::initMenuBar()
{
bool ret = true;
QMenuBar* mb = menuBar();
ret = ret && initFileMenu(mb);
ret = ret && initEidtMenu(mb);
ret = ret && initFormatMenu(mb);
ret = ret && initViewMenu(mb);
ret = ret && initHelpMenu(mb);
return ret;
}
bool MainWindow::initFileMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("File(&F)");
bool ret = (menu != NULL);
if(ret)
{
QAction* action = NULL;
ret = ret && makeAciton(action, "New(N)", Qt::CTRL + Qt::Key_N);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Open(O)", Qt::CTRL + Qt::Key_O);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Save(S)", Qt::CTRL + Qt::Key_S);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Save As(A)", NULL);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
menu->addSeparator();
ret = ret && makeAciton(action, "Page Setting(U)", NULL);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Print(P)", Qt::CTRL + Qt::Key_P);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
menu->addSeparator();
ret = ret && makeAciton(action, "Exit(X)", Qt::CTRL + Qt::Key_X);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
}
if( ret )
{
mb->addMenu(menu); // add Menu add to application Menu Bar
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initEidtMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("Eidt(&E)");
bool ret = (menu != NULL);
if(ret)
{
QAction* action = NULL;
ret = ret && makeAciton(action, "Undo(U)", Qt::CTRL + Qt::Key_Z);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
menu->addSeparator();
ret = ret && makeAciton(action, "Cut(T)", Qt::CTRL + Qt::Key_X);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Copy(C)", Qt::CTRL + Qt::Key_C);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Paste(P)", Qt::CTRL + Qt::Key_V);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Delere(L)", Qt::Key_Delete);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
menu->addSeparator();
ret = ret && makeAciton(action, "Find(F)", Qt::CTRL + Qt::Key_F);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Find Next(N)", Qt::Key_F3);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Replace(R)", Qt::CTRL + Qt::Key_H);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Goto(G)", Qt::CTRL + Qt::Key_G);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
menu->addSeparator();
ret = ret && makeAciton(action, "Select All(A)", Qt::CTRL + Qt::Key_A);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Time/Data(D)", Qt::Key_F5);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
}
if( ret )
{
mb->addMenu(menu); // add Menu add to application Menu Bar
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initFormatMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("Format(&O)");
bool ret = (menu != NULL);
if(ret)
{
QAction* action = NULL;
ret = ret && makeAciton(action, "Warp(W)", NULL);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
ret = ret && makeAciton(action, "Font(F)", NULL);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
}
if( ret )
{
mb->addMenu(menu); // add Menu add to application Menu Bar
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initViewMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("View(&V)");
bool ret = (menu != NULL);
if(ret)
{
QAction* action = NULL;
ret = ret && makeAciton(action, "Setting(S)", NULL);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
}
if( ret )
{
mb->addMenu(menu); // add Menu add to application Menu Bar
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initHelpMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("Help(&H)");
bool ret = (menu != NULL);
if(ret)
{
QAction* action = NULL;
ret = ret && makeAciton(action, "Helping(H)", NULL);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
menu->addSeparator();
ret = ret && makeAciton(action, "About NotePad(A)", NULL);
if( ret )
{
menu->addAction(action); // add Action item to Menu
}
}
if( ret )
{
mb->addMenu(menu); // add Menu add to application Menu Bar
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::makeAciton(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()
{
}
main.cpp
#include <QtGui/QApplication>
#include "MainWindow.h"
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;
}
输出结果:
9. 小结
- 主窗口是与用户进行长时间交互的顶层窗口
- 主窗口通常是应用程序启动后显示的第一个窗口
-
QMainWindow
是Qt中主窗口的基类 -
QMainWindow
是一种容器类型的窗口组件 -
QMainWindow
中封装了菜单栏,工具栏,状态栏等组件
声明:此文章仅是本人在学习狄泰QT实验分析课程所做的笔记,文章中包含狄泰软件资料内容,一切版权归狄泰软件所有!
实验环境:ubuntu10 + Qt Creator2.4.1 + Qt SDK 4.7.4