Qt实现基本QMainWindow主窗口程序

这个实验用Qt实现基本QMainWindow主窗口


先上实验效果图

    

打开一个文件,读取文件类容




详细步骤:


1.打开Qt creator新建MainWindow工程




右键工程名添加新文件,main文件,后缀自动补全后为main.cpp






选中工程,再次右键,选择模板的时候选择C++类,添加一个MainWindow类,该类继承字QMainWindow





代码部分


[cpp]  view plain copy
  1. #ifndef MAINWINDOW_H  
  2. #define MAINWINDOW_H  
  3.   
  4. #include <QMainWindow>  
  5. #include <QApplication>  
  6. #include <QAction>  
  7. #include <QMenu>  
  8. #include <QFileDialog>  
  9. #include <QFile>  
  10. #include <QTextStream>  
  11. #include <QToolBar>  
  12. #include <QTextEdit>  
  13. #include <QMainWindow>  
  14. #include <QString>  
  15. #include <QMenuBar>  
  16.   
  17.   
  18. class MainWindow : public QMainWindow  
  19. {  
  20.     Q_OBJECT  
  21.   
  22. public:  
  23.     explicit MainWindow(QWidget *parent = 0);  
  24.     ~MainWindow();  
  25.   
  26.     void createMenus();  
  27.     void createActions();  
  28.     void createToolBars();  
  29.     void loadFile(QString fileName);  
  30.   
  31. public slots:  
  32.     void slotNewFile();  
  33.     void slotOpenFile();  
  34.     void slotSaveFile();  
  35.   
  36. private:  
  37.   
  38.   
  39.     QMenu *fileMenu;  
  40.     QMenu *editMenu;  
  41.     QMenu *aboutMenu;  
  42.     QString fileName;  
  43.   
  44.     QToolBar *fileTool;  
  45.     QToolBar *editTool;  
  46.   
  47.     QAction *fileOpenAction;  
  48.     QAction *fileNewAction;  
  49.     QAction *fileSaveAction;  
  50.     QAction *exitAction;  
  51.     QAction *copyAction;  
  52.     QAction *cutAction;  
  53.     QAction *pasteAction;  
  54.     QAction *aboutAction;  
  55.   
  56.     QTextEdit *text;  
  57.   
  58.   
  59. };  
  60.   
  61. #endif // MAINWINDOW_H  

[cpp]  view plain copy
  1. #include "mainwindow.h"  
  2.   
  3.   
  4. MainWindow::MainWindow(QWidget *parent) :  
  5.     QMainWindow(parent)  
  6.   
  7. {  
  8.     //设置主窗口标题  
  9.         setWindowTitle(tr("QMainWindow"));  
  10.         text = new QTextEdit(this);  
  11.         setCentralWidget(text);  
  12.   
  13.         createActions();  
  14.         createMenus();  
  15.         createToolBars();  
  16.   
  17. }  
  18.   
  19. MainWindow::~MainWindow()  
  20. {  
  21.   
  22. }  
  23.   
  24. void MainWindow::createActions()  
  25. {  
  26.     //通过图片路径来实现添加Qt资源文件目的  
  27.     fileOpenAction = new QAction(QIcon("C:/qt/Mwindow/open.png"),tr("Open"),this);  
  28.     fileOpenAction->setShortcut(tr("Ctrl+O"));  
  29.     fileOpenAction->setStatusTip(tr("Open a file"));  
  30.     connect(fileOpenAction,SIGNAL(triggered()),this,SLOT(slotOpenFile()));  
  31.   
  32.     fileNewAction = new QAction(QIcon("C:/qt/Mwindow/new.png"),tr("New"),this);  
  33.     fileNewAction->setShortcut(tr("Ctrl+N"));  
  34.     fileNewAction->setStatusTip(tr("new file"));  
  35.     connect(fileNewAction,SIGNAL(triggered()),this,SLOT(slotNewFile()));  
  36.   
  37.     fileSaveAction = new QAction(QIcon("C:/qt/Mwindow/save.png"),tr("Save"),this);  
  38.     fileSaveAction->setShortcut(tr("Ctrl+S"));  
  39.     fileNewAction->setStatusTip(tr("save file"));  
  40.     connect(fileNewAction,SIGNAL(triggered()),this,SLOT(slotSaveFile()));  
  41.   
  42.     exitAction = new QAction(tr("Exit"),this);  
  43.     exitAction->setShortcut(tr("Ctrl+Q"));  
  44.     exitAction->setStatusTip(tr("exit"));  
  45.     connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));  
  46.   
  47.     cutAction = new QAction(QIcon("C:/qt/Mwindow/cut.png"),tr("Cut"),this);  
  48.     cutAction->setShortcut(tr("Ctrl+X"));  
  49.     cutAction->setStatusTip(tr("cut to clipboard"));  
  50.     connect(cutAction,SIGNAL(triggered()),text,SLOT(cut()));  
  51.   
  52.     copyAction = new QAction(QIcon("C:/qt/Mwindow/copy.png"),tr("Copy"),this);  
  53.     copyAction->setShortcut(tr("Ctrl+C"));  
  54.     copyAction->setStatusTip(tr("copy to clipboard"));  
  55.     connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));  
  56.   
  57.     pasteAction = new QAction(QIcon("C:/qt/Mwindow/paste.png"),tr("paste"),this);  
  58.     pasteAction->setShortcut(tr("Ctrl+V"));  
  59.     pasteAction->setStatusTip(tr("paste clipboard to selection"));  
  60.     connect(pasteAction,SIGNAL(triggered()),this,SLOT(paste()));  
  61.   
  62.     aboutAction = new QAction(tr("About"),this);  
  63.     connect(aboutAction,SIGNAL(triggered()),this,SLOT(slotAbout()));  
  64.   
  65. }  
  66.   
  67. void MainWindow::createMenus()  
  68. {  
  69.     fileMenu = menuBar()->addMenu(tr("File"));  
  70.     fileMenu->addAction(fileNewAction);  
  71.     fileMenu->addAction(fileOpenAction);  
  72.     fileMenu->addAction(fileSaveAction);  
  73.     fileMenu->addAction(exitAction);  
  74.   
  75.   
  76.     editMenu = menuBar()->addMenu(tr("Edit"));  
  77.     editMenu->addAction(copyAction);  
  78.     editMenu->addAction(cutAction);  
  79.     editMenu->addAction(pasteAction);  
  80.   
  81.   
  82.     aboutMenu = menuBar()->addMenu(tr("Help"));  
  83.     aboutMenu->addAction(aboutAction);  
  84.   
  85. }  
  86.   
  87. void MainWindow::createToolBars()  
  88. {  
  89.     fileTool = addToolBar("File");  
  90.     fileTool->addAction(fileNewAction);  
  91.     fileTool->addAction(fileOpenAction);  
  92.     fileTool->addAction(fileSaveAction);  
  93.   
  94.     editTool = addToolBar("Edit");  
  95.     editTool->addAction(copyAction);  
  96.     editTool->addAction(cutAction);  
  97.     editTool->addAction(pasteAction);  
  98. }  
  99.   
  100. void MainWindow::slotNewFile()  
  101. {  
  102.     MainWindow *newWin = new MainWindow;  
  103.     newWin->show();  
  104. }  
  105.   
  106. void MainWindow::slotOpenFile()  
  107. {  
  108.     fileName = QFileDialog::getOpenFileName(this);  
  109.     if(!fileName.isEmpty())  
  110.     {  
  111.         if(text->document()->isEmpty())  
  112.         {  
  113.             loadFile(fileName);  
  114.         }  
  115.         else  
  116.         {  
  117.             MainWindow *newWin = new MainWindow;  
  118.             newWin->show();  
  119.             newWin->loadFile(fileName);  
  120.         }  
  121.   
  122.     }  
  123.   
  124. }  
  125.   
  126.   
  127. void MainWindow::loadFile(QString fileName)  
  128. {  
  129.     printf("file name:%s\n",fileName.data());  
  130.     QFile file(fileName);  
  131.     if(file.open(QIODevice::ReadOnly|QIODevice::Text))  
  132.     {  
  133.         QTextStream textStream(&file);  
  134.         while(!textStream.atEnd())  
  135.         {  
  136.             text->append(textStream.readLine());  
  137.             printf("read line.\n");  
  138.         }  
  139.         printf("end\n");  
  140.   
  141.     }  
  142. }  



[cpp]  view plain copy
  1. #include <QtGui/QApplication>  
  2. #include "mainwindow.h"  
  3.   
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication a(argc, argv);  
  8.     MainWindow w;  
  9.     w.show();  
  10.   
  11.   
  12.     return a.exec();  
  13. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值