30.QT IDE编写

  • mainwindow.h
      1 #ifndef MAINWINDOW_H
      2 #define MAINWINDOW_H
      3 
      4 #include <QMainWindow>
      5 #include <QTextEdit>
      6 #include <QMenu>
      7 #include <QMenuBar>
      8 #include <QAction>
      9 #include <QFileDialog>
     10 #include <QMessageBox>
     11 #include <iostream>
     12 #include <fstream>
     13 #include <cstdlib>
     14 
     15 #include <QImage>
     16 #include <QLabel>
     17 #include <QMenu>
     18 #include <QMenuBar>
     19 #include <QComboBox>
     20 #include <QSpinBox>
     21 #include <QToolBar>
     22 #include <QFontComboBox>
     23 #include <QToolButton>
     24 #include <QTextCharFormat>
     25 using namespace std;
     26 
     27 namespace Ui {
     28 class MainWindow;
     29 }
     30 
     31 class MainWindow : public QMainWindow
     32 {
     33     Q_OBJECT
     34 
     35 public:
     36     explicit MainWindow(QWidget *parent = 0);
     37     ~MainWindow();
     38     void createActions();
     39     void createMenus();
     40     void createToolBars();
     41 
     42 private:
     43     Ui::MainWindow *ui;
     44     QString filename;
     45     //文本编辑
     46     QTextEdit *text;
     47     //菜单
     48     QMenu *file;
     49         //菜单选项
     50         QAction *Afile_open;
     51         QAction *Afile_close;
     52         QAction *Afile_save;
     53         QAction *Afile_exit;
     54 
     55     QMenu *edit;
     56         QAction *Afile_selall;
     57         QAction *Afile_cut;
     58         QAction *Afile_copy;
     59         QAction *Afile_paste;
     60 
     61     QMenu *build;
     62         QAction *Abuild_it;
     63         QAction *Abuild_andrun;
     64 
     65     QMenu *about;
     66         QAction *Aabout_it;
     67 
     68 private:
     69         QMenu *fileMenu;                                   //各项菜单栏
     70             QMenu *zoomMenu;
     71             QMenu *rotateMenu;
     72             QMenu *mirrorMenu;
     73 
     74             QImage img;
     75             QString fileName;
     76             //编辑显示窗口
     77            // ShowWidget *showWidget;
     78 
     79             QAction *openFileAction;                         //文件菜单项
     80             QAction *NewFileAction;
     81             QAction *PrintTextAction;
     82             QAction *PrintImageAction;
     83             QAction *exitAction;
     84 
     85             QAction *copyAction;                              //编辑菜单项
     86             QAction *cutAction;
     87             QAction *pasteAction;
     88             QAction *aboutAction;
     89             QAction *zoomInAction;
     90             QAction *zoomOutAction;
     91 
     92             QAction *rotate90Action;                         //旋转菜单项
     93             QAction *rotate180Action;
     94             QAction *rotate270Action;
     95 
     96             QAction *mirrorVerticalAction;                  //镜像菜单项
     97             QAction *mirrorHorizontalAction;
     98 
     99             QAction *undoAction;
    100             QAction *redoAction;
    101 
    102             QToolBar *fileTool;                              //工具栏
    103             QToolBar *zoomTool;
    104             QToolBar *rotateTool;
    105             QToolBar *mirrorTool;
    106 
    107             QToolBar *doToolBar;
    108 
    109             QLabel *fontLabel1;                             //字体设置项
    110             QFontComboBox *fontComboBox;
    111             QLabel *fontLabel2;
    112             QComboBox *sizeComboBox;
    113             QToolButton *boldBtn;
    114             QToolButton *italicBtn;
    115             QToolButton *underlineBtn;
    116             QToolButton *colorBtn;
    117 
    118             QToolBar *fontToolBar;                          //字体工具栏
    119 
    120             QLabel *listLabel;                              //排序设置项
    121             QComboBox *listComboBox;
    122             QActionGroup *actGrp;
    123             QAction *leftAction;
    124             QAction *rightAction;
    125             QAction *centerAction;
    126             QAction *justifyAction;
    127 
    128             QToolBar *listToolBar;                          //排序工具栏
    129 private slots:
    130     void file_open();
    131     void file_close();
    132     void file_save();
    133     void file_exit();
    134     void file_selall();
    135     void file_cut();
    136     void file_copy();
    137     void file_paste();
    138     void build_it();
    139     void build_andrun();
    140     void about_it();
    141 
    142 };
    143 
    144 #endif // MAINWINDOW_H

     

  • mainwindow.cpp
      1 #include "mainwindow.h"
      2 #include "ui_mainwindow.h"
      3 
      4 MainWindow::MainWindow(QWidget *parent) :
      5     QMainWindow(parent),
      6     ui(new Ui::MainWindow)
      7 {
      8 
      9     ui->setupUi(this);
     10     //设置文本编辑器
     11     text = new QTextEdit;
     12     QFont font;
     13     font.setPixelSize(30);
     14     text->setFont(font);
     15     QColor qcolor;
     16     qcolor.setRgb(255,255,255);
     17     this->setCentralWidget(text);
     18 
     19     //设置菜单
     20     file = this->menuBar()->addMenu("文件");
     21     edit = this->menuBar()->addMenu("编辑");
     22     build = this->menuBar()->addMenu("构建");
     23     about = this->menuBar()->addMenu("关于");
     24 
     25     //about菜单
     26     {
     27         Aabout_it = new QAction("关于",this);
     28         about->addAction(Aabout_it);
     29         connect(Aabout_it,SIGNAL(triggered(bool)),this,SLOT(about_it()));
     30     }
     31 
     32     //open菜单
     33     {
     34         Afile_open = new QAction("打开文件",this);
     35         file->addAction(Afile_open);
     36         connect(Afile_open,SIGNAL(triggered(bool)),this,SLOT(file_open()));
     37 
     38         Afile_close = new QAction("关闭文件",this);
     39         file->addAction(Afile_close);
     40         connect(Afile_close,SIGNAL(triggered(bool)),this,SLOT(file_close()));
     41 
     42         Afile_save = new QAction("保存文件",this);
     43         file->addAction(Afile_save);
     44         connect( Afile_save,SIGNAL(triggered(bool)),this,SLOT(file_save()));
     45 
     46         Afile_exit = new QAction("退出",this);
     47         file->addAction(Afile_exit);
     48         connect(Afile_exit,SIGNAL(triggered(bool)),this,SLOT(file_exit()));
     49     }
     50 
     51     //编辑菜单
     52     {
     53         Afile_selall = new QAction("全选",this);
     54         edit->addAction(Afile_selall);
     55         connect(Afile_selall,SIGNAL(triggered(bool)),this,SLOT(file_selall()));
     56 
     57         Afile_cut = new QAction("剪切",this);
     58         edit->addAction(Afile_cut);
     59         connect(Afile_cut,SIGNAL(triggered(bool)),this,SLOT(file_cut()));
     60 
     61         Afile_copy = new QAction("复制",this);
     62         edit->addAction(Afile_copy);
     63         connect(Afile_copy,SIGNAL(triggered(bool)),this,SLOT(file_copy()));
     64 
     65         Afile_paste = new QAction("粘贴",this);
     66         edit->addAction(Afile_paste);
     67         connect(Afile_paste,SIGNAL(triggered(bool)),this,SLOT(file_paste()));
     68     }
     69 
     70     {
     71         Abuild_it = new QAction("编译",this);
     72         build->addAction(Abuild_it);
     73         connect(Abuild_it,SIGNAL(triggered(bool)),this,SLOT(build_it()));
     74 
     75         Abuild_andrun = new QAction("编译执行",this);
     76         build->addAction(Abuild_andrun);
     77         connect(Abuild_andrun,SIGNAL(triggered(bool)),this,SLOT(build_andrun()));
     78     }
     79 
     80     this->setWindowTitle("简易编译器");
     81 
     82     //showWidget =new ShowWidget(this);
     83      //   setCentralWidget(showWidget);
     84         //在工具栏上嵌入控件
     85         //设置字体
     86         fontLabel1 =new QLabel(tr("字体:"));
     87         fontComboBox =new QFontComboBox;
     88         fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
     89 
     90         fontLabel2 =new QLabel(tr("字号:"));
     91         sizeComboBox =new QComboBox;
     92         QFontDatabase db;
     93         foreach(int size,db.standardSizes())
     94             sizeComboBox->addItem(QString::number(size));
     95 
     96         boldBtn =new QToolButton;
     97         boldBtn->setIcon(QIcon("bold.png"));
     98         boldBtn->setCheckable(true);
     99         italicBtn =new QToolButton;
    100         italicBtn->setIcon(QIcon("italic.png"));
    101         italicBtn->setCheckable(true);
    102 
    103         underlineBtn =new QToolButton;
    104         underlineBtn->setIcon(QIcon("underline.png"));
    105         underlineBtn->setCheckable(true);
    106 
    107         colorBtn =new QToolButton;
    108         colorBtn->setIcon(QIcon("color.png"));
    109         colorBtn->setCheckable(true);
    110 
    111         //排序
    112         listLabel =new QLabel(tr("排序"));
    113         listComboBox =new QComboBox;
    114         listComboBox->addItem("Standard");
    115         listComboBox->addItem("QTextListFormat::ListDisc");
    116         listComboBox->addItem("QTextListFormat::ListCircle");
    117         listComboBox->addItem("QTextListFormat::ListSquare");
    118         listComboBox->addItem("QTextListFormat::ListDecimal");
    119         listComboBox->addItem("QTextListFormat::ListLowerAlpha");
    120         listComboBox->addItem("QTextListFormat::ListUpperAlpha");
    121         listComboBox->addItem("QTextListFormat::ListLowerRoman");
    122         listComboBox->addItem("QTextListFormat::ListUpperRoman");
    123 
    124         createActions();
    125         createMenus();
    126         createToolBars();
    127 
    128         if(img.load("image.png"))
    129         {
    130             //showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
    131         }
    132 
    133 }
    134 
    135 MainWindow::~MainWindow()
    136 {
    137     delete ui;
    138 }
    139 
    140 void MainWindow::file_open()
    141 {
    142     filename =QFileDialog::getOpenFileName(this,"hello","*.*");
    143     if(filename.isEmpty())
    144     {
    145         QMessageBox::information(this,"提示","注意选择一个文件");
    146         return;
    147     }
    148     fstream fin(filename.toStdString().c_str());
    149     if(!fin)
    150     {
    151         QMessageBox::information(this,"提示","打开文件失败");
    152         return;
    153     }
    154 
    155     while(!fin.eof())
    156     {
    157         char buf[2014]={0};
    158         fin.getline(buf,2014);
    159         text->append(buf);
    160     }
    161     fin.close();
    162 }
    163 
    164 void MainWindow::file_close()
    165 {
    166     text->setText("");
    167     filename ="";
    168 }
    169 
    170 void MainWindow::file_save()
    171 {
    172     filename = QFileDialog::getSaveFileName();
    173     if(filename.isEmpty())
    174     {
    175         QMessageBox::information(this,"提示","打开文件失败");
    176         return;
    177     }
    178 
    179     QString info =text->toPlainText().toStdString().c_str();
    180     //写入
    181     ofstream fout(filename.toStdString().c_str());
    182     if(!fout)
    183     {
    184         QMessageBox::information(this,"提示","打开文件失败");
    185         return;
    186     }
    187     fout.write(info.toStdString().c_str(),strlen(info.toStdString().c_str()));
    188     fout.close();
    189 }
    190 
    191 void MainWindow::file_exit()
    192 {
    193     exit(0);
    194 }
    195 
    196 void MainWindow::file_selall()
    197 {
    198     text->selectAll();
    199 }
    200 
    201 void MainWindow::file_cut()
    202 {
    203     text->cut();
    204 }
    205 
    206 void MainWindow::file_copy()
    207 {
    208     text->copy();
    209 }
    210 
    211 void MainWindow::file_paste()
    212 {
    213     text->paste();
    214 }
    215 
    216 void MainWindow::build_it()
    217 {
    218     QString destexename = filename;
    219     bool iscpp = destexename.contains(".cpp");
    220     QString cmd;
    221     if(iscpp)
    222     {
    223         destexename.replace(".cpp",".exe");
    224         cmd = "g++ -o " + destexename+" " +filename;
    225     }
    226     else
    227     {
    228         destexename.replace(".c",".exe");
    229         cmd = "gcc -o "+destexename+" "+filename;
    230     }
    231     system(cmd.toStdString().c_str());
    232 }
    233 
    234 void MainWindow::build_andrun()
    235 {
    236     QString destexename = filename;
    237     bool iscpp = destexename.contains(".cpp");
    238     QString cmd;
    239     if(iscpp)
    240     {
    241         destexename.replace(".cpp",".exe");
    242         cmd = "g++ -o "+filename+" "+destexename;
    243     }
    244     else
    245     {
    246         destexename.replace(".c",".exe");
    247         cmd = "gcc -o "+filename+" "+destexename;
    248     }
    249     system(cmd.toStdString().c_str());
    250 
    251     system(destexename.toStdString().c_str());
    252 }
    253 
    254 void MainWindow::about_it()
    255 {
    256     QMessageBox::information(this,"hello","版权所有,盗版不究");
    257 }
    258 
    259 void MainWindow::createActions()
    260 {
    261     //"打开"动作
    262     openFileAction =new QAction(QIcon("open.png"),tr("打开"),this);
    263     openFileAction->setShortcut(tr("Ctrl+O"));
    264     openFileAction->setStatusTip(tr("打开一个文件"));
    265    // connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile()));
    266 
    267     //"新建"动作
    268     NewFileAction =new QAction(QIcon("new.png"),tr("新建"),this);
    269     NewFileAction->setShortcut(tr("Ctrl+N"));
    270     NewFileAction->setStatusTip(tr("新建一个文件"));
    271     //connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile()));
    272 
    273     //"退出"动作
    274     exitAction =new QAction(tr("退出"),this);
    275     exitAction->setShortcut(tr("Ctrl+Q"));
    276     exitAction->setStatusTip(tr("退出程序"));
    277     //connect(exitAction,SIGNAL(triggered()),this,SLOT(close()));
    278 
    279     //"复制"动作
    280     copyAction =new QAction(QIcon("copy.png"),tr("复制"),this);
    281     copyAction->setShortcut(tr("Ctrl+C"));
    282     copyAction->setStatusTip(tr("复制文件"));
    283    // connect(copyAction,SIGNAL(triggered()),showWidget->text,SLOT(copy()));
    284 
    285     //"剪切"动作
    286     cutAction =new QAction(QIcon("cut.png"),tr("剪切"),this);
    287     cutAction->setShortcut(tr("Ctrl+X"));
    288     cutAction->setStatusTip(tr("剪切文件"));
    289     //connect(cutAction,SIGNAL(triggered()),showWidget->text,SLOT(cut()));
    290 
    291     //"粘贴"动作
    292     pasteAction =new QAction(QIcon("paste.png"),tr("粘贴"),this);
    293     pasteAction->setShortcut(tr("Ctrl+V"));
    294     pasteAction->setStatusTip(tr("粘贴文件"));
    295   //  connect(pasteAction,SIGNAL(triggered()),showWidget->text,SLOT(paste()));
    296 
    297     //"关于"动作
    298     aboutAction =new QAction(tr("关于"),this);
    299    // connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt()));
    300 
    301     //"打印文本"动作
    302     PrintTextAction =new QAction(QIcon("printText.png"),tr("打印文本"), this);
    303     PrintTextAction->setStatusTip(tr("打印一个文本"));
    304    // connect(PrintTextAction,SIGNAL(triggered()),this,SLOT(ShowPrintText()));
    305 
    306     //"打印图像"动作
    307     PrintImageAction =new QAction(QIcon("printImage.png"),tr("打印图像"), this);
    308     PrintImageAction->setStatusTip(tr("打印一幅图像"));
    309   //  connect(PrintImageAction,SIGNAL(triggered()),this,SLOT(ShowPrintImage()));
    310 
    311     //"放大"动作
    312     zoomInAction =new QAction(QIcon("zoomin.png"),tr("放大"),this);
    313     zoomInAction->setStatusTip(tr("放大一张图片"));
    314    // connect(zoomInAction,SIGNAL(triggered()),this,SLOT(ShowZoomIn()));
    315 
    316     //"缩小"动作
    317     zoomOutAction =new QAction(QIcon("zoomout.png"),tr("缩小"),this);
    318     zoomOutAction->setStatusTip(tr("缩小一张图片"));
    319    // connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(ShowZoomOut()));
    320 
    321     //实现图像旋转的动作(Action)
    322     //旋转90°
    323     rotate90Action =new QAction(QIcon("rotate90.png"),tr("旋转90°"),this);
    324     rotate90Action->setStatusTip(tr("将一幅图旋转90°"));
    325    // connect(rotate90Action,SIGNAL(triggered()),this,SLOT(ShowRotate90()));
    326 
    327     //旋转180°
    328     rotate180Action =new QAction(QIcon("rotate180.png"),tr("旋转180°"), this);
    329     rotate180Action->setStatusTip(tr("将一幅图旋转180°"));
    330     connect(rotate180Action,SIGNAL(triggered()),this,SLOT(ShowRotate180()));
    331 
    332     //旋转270°
    333     rotate270Action =new QAction(QIcon("rotate270.png"),tr("旋转270°"), this);
    334     rotate270Action->setStatusTip(tr("将一幅图旋转270°"));
    335    // connect(rotate270Action,SIGNAL(triggered()),this,SLOT(ShowRotate270()));
    336 
    337     //实现图像镜像的动作(Action)
    338     //纵向镜像
    339     mirrorVerticalAction =new QAction(tr ("纵向镜像"),this);
    340     mirrorVerticalAction->setStatusTip(tr("对一张图作纵向镜像"));
    341    // connect(mirrorVerticalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorVertical()));
    342 
    343     //横向镜像
    344     mirrorHorizontalAction =new QAction(tr("横向镜像"),this);
    345     mirrorHorizontalAction->setStatusTip(tr("对一张图作横向镜像"));
    346     //connect(mirrorHorizontalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorHorizontal()));
    347 
    348     //排序:左对齐、右对齐、居中和两端对齐
    349     actGrp =new QActionGroup(this);
    350 
    351     leftAction =new QAction(QIcon("left.png"),"左对齐",actGrp);
    352     leftAction->setCheckable(true);
    353 
    354     rightAction =new QAction(QIcon("right.png"),"右对齐",actGrp);
    355     rightAction->setCheckable(true);
    356 
    357     centerAction =new QAction(QIcon("center.png"),"居中",actGrp);
    358     centerAction->setCheckable(true);
    359 
    360     justifyAction =new QAction(QIcon("justify.png"),"两端对齐",actGrp);
    361     justifyAction->setCheckable(true);
    362 
    363    // connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(ShowAlignment(QAction*)));
    364 
    365     //实现撤销和重做的动作(Action)
    366     //撤销和重做
    367     undoAction =new QAction(QIcon("undo.png"),"撤销",this);
    368    // connect(undoAction,SIGNAL(triggered()),showWidget->text,SLOT(undo()));
    369     redoAction =new QAction(QIcon("redo.png"),"重做",this);
    370     //connect(redoAction,SIGNAL(triggered()),showWidget->text,SLOT(redo()));
    371 }
    372 
    373 void MainWindow::createMenus()
    374 {
    375     //文件菜单
    376     fileMenu =menuBar()->addMenu(tr("文件"));
    377     fileMenu->addAction(openFileAction);
    378     fileMenu->addAction(NewFileAction);
    379     fileMenu->addAction(PrintTextAction);
    380     fileMenu->addAction(PrintImageAction);
    381     fileMenu->addSeparator();
    382     fileMenu->addAction(exitAction);
    383 
    384     //缩放菜单
    385     zoomMenu =menuBar()->addMenu(tr("编辑"));
    386     zoomMenu->addAction(copyAction);
    387     zoomMenu->addAction(cutAction);
    388     zoomMenu->addAction(pasteAction);
    389     zoomMenu->addAction(aboutAction);
    390     zoomMenu->addSeparator();
    391     zoomMenu->addAction(zoomInAction);
    392     zoomMenu->addAction(zoomOutAction);
    393 
    394     //旋转菜单
    395     rotateMenu =menuBar()->addMenu(tr("旋转"));
    396     rotateMenu->addAction(rotate90Action);
    397     rotateMenu->addAction(rotate180Action);
    398     rotateMenu->addAction(rotate270Action);
    399 
    400     //镜像菜单
    401     mirrorMenu =menuBar()->addMenu(tr("镜像"));
    402     mirrorMenu->addAction(mirrorVerticalAction);
    403     mirrorMenu->addAction(mirrorHorizontalAction);
    404 }
    405 
    406 void MainWindow::createToolBars()
    407 {
    408     //文件工具条
    409     fileTool =addToolBar("File");
    410     fileTool->addAction(openFileAction);
    411     fileTool->addAction(NewFileAction);
    412     fileTool->addAction(PrintTextAction);
    413     fileTool->addAction(PrintImageAction);
    414 
    415     //编辑工具条
    416     zoomTool =addToolBar("Edit");
    417     zoomTool->addAction(copyAction);
    418     zoomTool->addAction(cutAction);
    419     zoomTool->addAction(pasteAction);
    420     zoomTool->addSeparator();
    421     zoomTool->addAction(zoomInAction);
    422     zoomTool->addAction(zoomOutAction);
    423 
    424     //旋转工具条
    425     rotateTool =addToolBar("rotate");
    426     rotateTool->addAction(rotate90Action);
    427     rotateTool->addAction(rotate180Action);
    428     rotateTool->addAction(rotate270Action);
    429 
    430     //撤销和重做工具条
    431     doToolBar =addToolBar("doEdit");
    432     doToolBar->addAction(undoAction);
    433     doToolBar->addAction(redoAction);
    434 
    435     //字体工具条
    436     fontToolBar =addToolBar("Font");
    437     fontToolBar->addWidget(fontLabel1);
    438     fontToolBar->addWidget(fontComboBox);
    439     fontToolBar->addWidget(fontLabel2);
    440     fontToolBar->addWidget(sizeComboBox);
    441     fontToolBar->addSeparator();
    442     fontToolBar->addWidget(boldBtn);
    443     fontToolBar->addWidget(italicBtn);
    444     fontToolBar->addWidget(underlineBtn);
    445     fontToolBar->addSeparator();
    446     fontToolBar->addWidget(colorBtn);
    447 
    448     //排序工具条
    449     listToolBar =addToolBar("list");
    450     listToolBar->addWidget(listLabel);
    451     listToolBar->addWidget(listComboBox);
    452     listToolBar->addSeparator();
    453     listToolBar->addActions(actGrp->actions());
    454 }

     

  • main.cpp
     1 #include "mainwindow.h"
     2 #include <QApplication>
     3 
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     QApplication a(argc, argv);
     8     MainWindow w;
     9     w.show();
    10     w.resize(1024,768);
    11     return a.exec();
    12 }

     

posted @ 2018-04-10 23:37  喵小喵~  阅读(796)  评论(0编辑  收藏  举报