Qt之主窗口设计——基于QMainWindow主窗口程序

image

mainwindow.h

  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.   
  4. #include <QtGui>  
  5.   
  6.   
  7. class MainWindow : public QMainWindow  
  8. {  
  9.     Q_OBJECT  
  10.   
  11. public:  
  12.     MainWindow(QWidget *parent = 0, Qt::WFlags flags = 0);  
  13.     ~MainWindow();  
  14.   
  15.     void createMenus();     //创建菜单  
  16.     void createActions();   //创建动作  
  17.     void createToolBars();  //创建工具栏  
  18. public slots:  
  19.     void sl_NewFile();  
  20.     void sl_OpenFile();  
  21.     void sl_SaveFile();  
  22.     void sl_About();  
  23. protected:  
  24.     void loadFile(QString);  
  25.   
  26. private:  
  27.     QMenu *fileMenu;  
  28.     QMenu *editMenu;  
  29.     QMenu *aboutMenu;  
  30.   
  31.     QToolBar *fileTool;  
  32.     QToolBar *editTool;  
  33.   
  34.     QAction *fileOpenAction;  
  35.     QAction *fileNewAction;  
  36.     QAction *fileSaveAction;  
  37.     QAction *exitAction;  
  38.   
  39.     QAction *copyAction;  
  40.     QAction *cutAction;  
  41.     QAction *pasteAction;  
  42.   
  43.     QAction *aboutAction;  
  44.   
  45.     QTextEdit *edtText;   
  46. };  
  47.   
  48. #endif // MAINWINDOW_H  

mainwindow.cpp

  1. #include "mainwindow.h"  
  2. #include <QTextStream>  
  3. #include <QFile>  
  4.   
  5. MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)  
  6.     : QMainWindow(parent, flags)  
  7. {  
  8.     setWindowTitle(tr("QMainWindow"));  
  9.     edtText = new QTextEdit(this);  
  10.     setCentralWidget(edtText);  
  11.   
  12.     createActions();  
  13.     createMenus();  
  14.     createToolBars();  
  15. }  
  16.   
  17. MainWindow::~MainWindow()  
  18. {  
  19.   
  20. }  
  21.   
  22. void MainWindow::createActions()  
  23. {  
  24.     //"打开"动作  
  25.     fileOpenAction = new QAction(QIcon("images/open.png"),tr("Open"),this);  
  26.     fileOpenAction->setShortcut(tr("Ctrl+0"));  
  27.     fileOpenAction->setStatusTip(tr("open a file"));  
  28.     connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(sl_OpenFile()));  
  29.   
  30.     //"新建"动作  
  31.     fileNewAction = new QAction(QIcon("images/new.png"),tr("New"),this);  
  32.     fileNewAction->setShortcut(tr("Ctrl+N"));  
  33.     fileNewAction->setStatusTip(tr("new file"));  
  34.     connect(fileNewAction,SIGNAL(triggered()),this,SLOT(sl_NewFile()));  
  35.   
  36.     //"保存"动作  
  37.     fileSaveAction = new QAction(QIcon("images/save.png"),tr("Save"),this);  
  38.     fileSaveAction->setShortcut(tr("Ctrl+S"));  
  39.     fileSaveAction->setStatusTip(tr("save file"));  
  40.     connect(fileSaveAction,SIGNAL(activated()),this,SLOT(sl_SaveFile()));  
  41.   
  42.     //"退出"动作  
  43.     exitAction = new QAction(tr("Exit"),this);  
  44.     exitAction->setShortcut(tr("Ctrl+Q"));  
  45.     exitAction->setStatusTip(tr("exit"));  
  46.     connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));  
  47.   
  48.     //"剪切"动作  
  49.     cutAction = new QAction(QIcon("images/cut.png"),tr("cut"),this);  
  50.     cutAction->setShortcut(tr("Ctrl+X"));  
  51.     cutAction->setStatusTip("cut to clipboard");  
  52.     connect(cutAction,SIGNAL(triggered()),this,SLOT(cut()));  
  53.   
  54.     //"复制"动作  
  55.     copyAction = new QAction(QIcon("images/copy.png"),tr("copy"),this);  
  56.     copyAction->setShortcut(tr("Ctrl+C"));  
  57.     copyAction->setStatusTip("copy to clipboard");  
  58.     connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));  
  59.   
  60.     //"粘贴"动作  
  61.     pasteAction = new QAction(QIcon("images/paste.png"),tr("paste"),this);  
  62.     pasteAction->setShortcut(tr("Ctrl+V"));  
  63.     pasteAction->setStatusTip("paste clipboard to selection");  
  64.     connect(pasteAction,SIGNAL(triggered()),this,SLOT(paste()));  
  65.   
  66.     //"关于"动作  
  67.     aboutAction = new QAction(tr("About"),this);  
  68.     connect(aboutAction,SIGNAL(triggered()),this,SLOT(sl_About()));  
  69. }  
  70.   
  71. void MainWindow::createMenus()  
  72. {  
  73.     //文件菜单  
  74.     fileMenu = menuBar()->addMenu(tr("File"));  
  75.     fileMenu->addAction(fileNewAction);  
  76.     fileMenu->addAction(fileOpenAction);  
  77.     fileMenu->addAction(fileSaveAction);  
  78.     fileMenu->addSeparator();  
  79.     fileMenu->addAction(exitAction);  
  80.   
  81.     //编辑菜单  
  82.     editMenu = menuBar()->addMenu(tr("Edit"));  
  83.     editMenu->addAction(copyAction);  
  84.     editMenu->addAction(cutAction);  
  85.     editMenu->addAction(pasteAction);  
  86.   
  87.     //帮助菜单  
  88.     aboutMenu = menuBar()->addMenu(tr("Help"));  
  89.     aboutMenu->addAction(aboutAction);  
  90. }  
  91.   
  92. void MainWindow::createToolBars()  
  93. {  
  94.     //文件工具栏  
  95.     fileTool = addToolBar("File");  
  96.     fileTool->addAction(fileNewAction);  
  97.     fileTool->addAction(fileOpenAction);  
  98.     fileTool->addAction(fileSaveAction);  
  99.     fileTool->setAllowedAreas(Qt::TopToolBarArea | Qt::LeftToolBarArea);  
  100.     fileTool->setMovable(false);  
  101.   
  102.     //编辑工具栏  
  103.     editTool = addToolBar("Edit");  
  104.     editTool->addAction(copyAction);  
  105.     editTool->addAction(cutAction);  
  106.     editTool->addAction(pasteAction);  
  107. }  
  108.   
  109. void MainWindow::sl_NewFile()  
  110. {  
  111.     MainWindow *newWin = new MainWindow;  
  112.     newWin->show();  
  113. }  
  114.   
  115. void MainWindow::sl_OpenFile()  
  116. {  
  117.     QString fileName = QFileDialog::getOpenFileName(this);  
  118.     if(!fileName.isEmpty())  
  119.     {  
  120.         if(edtText->document()->isEmpty())  
  121.         {  
  122.             loadFile(fileName);  
  123.         }  
  124.         else  
  125.         {  
  126.             MainWindow *newWin = new MainWindow;  
  127.             newWin->show();  
  128.             newWin->loadFile(fileName);  
  129.         }  
  130.     }  
  131. }  
  132.   
  133. void MainWindow::sl_SaveFile()  
  134. {  
  135.   
  136. }  
  137.   
  138. void MainWindow::sl_About()  
  139. {  
  140.   
  141. }  
  142.   
  143. void MainWindow::loadFile(QString fileName)  
  144. {  
  145.     QFile file(fileName);  
  146.     if(file.open(QIODevice::ReadOnly | QIODevice::Text))  
  147.     {  
  148.         QTextStream textStream(&file);  
  149.         while (!textStream.atEnd())  
  150.         {  
  151.             edtText->append(textStream.readLine());  
  152.         }  
  153.     }  
  154. }  

工具条是一个可移动的窗口,它可停靠的区域由QToolBar的allowAreas()函数决定的,它的参数包括:

Qt::LeftToolBarArea、Qt::RightToolBarArea、Qt::TopToolBarArea、Qt::BottomToolBarArea、Qt::AllToolBarArea.

(默认为Qt::AllToolBarArea,启动时默认出现于主窗口的顶部)。

setMovable()函数确定工具条的可移动性。默认参数为true,可移动。


作者: 韩兆新
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:  [02]Qt语言基础
标签:  Qt学习笔记

本文转自韩兆新博客博客园博客,原文链接:http://www.cnblogs.com/hanzhaoxin/archive/2012/11/24/2786283.html,如需转载请自行联系原作者
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值