• mainwindow.h

     #ifndef MAINWINDOW_H
    #define MAINWINDOW_H #include <QMainWindow>
    #include <QTextEdit>
    #include <QMenu>
    #include <QMenuBar>
    #include <QAction>
    #include <QFileDialog>
    #include <QMessageBox>
    #include <iostream>
    #include <fstream>
    #include <cstdlib> #include <QImage>
    #include <QLabel>
    #include <QMenu>
    #include <QMenuBar>
    #include <QComboBox>
    #include <QSpinBox>
    #include <QToolBar>
    #include <QFontComboBox>
    #include <QToolButton>
    #include <QTextCharFormat>
    using namespace std; namespace Ui {
    class MainWindow;
    } class MainWindow : public QMainWindow
    {
    Q_OBJECT public:
    explicit MainWindow(QWidget *parent = );
    ~MainWindow();
    void createActions();
    void createMenus();
    void createToolBars(); private:
    Ui::MainWindow *ui;
    QString filename;
    //文本编辑
    QTextEdit *text;
    //菜单
    QMenu *file;
    //菜单选项
    QAction *Afile_open;
    QAction *Afile_close;
    QAction *Afile_save;
    QAction *Afile_exit; QMenu *edit;
    QAction *Afile_selall;
    QAction *Afile_cut;
    QAction *Afile_copy;
    QAction *Afile_paste; QMenu *build;
    QAction *Abuild_it;
    QAction *Abuild_andrun; QMenu *about;
    QAction *Aabout_it; private:
    QMenu *fileMenu; //各项菜单栏
    QMenu *zoomMenu;
    QMenu *rotateMenu;
    QMenu *mirrorMenu; QImage img;
    QString fileName;
    //编辑显示窗口
    // ShowWidget *showWidget; QAction *openFileAction; //文件菜单项
    QAction *NewFileAction;
    QAction *PrintTextAction;
    QAction *PrintImageAction;
    QAction *exitAction; QAction *copyAction; //编辑菜单项
    QAction *cutAction;
    QAction *pasteAction;
    QAction *aboutAction;
    QAction *zoomInAction;
    QAction *zoomOutAction; QAction *rotate90Action; //旋转菜单项
    QAction *rotate180Action;
    QAction *rotate270Action; QAction *mirrorVerticalAction; //镜像菜单项
    QAction *mirrorHorizontalAction; QAction *undoAction;
    QAction *redoAction; QToolBar *fileTool; //工具栏
    QToolBar *zoomTool;
    QToolBar *rotateTool;
    QToolBar *mirrorTool; QToolBar *doToolBar; QLabel *fontLabel1; //字体设置项
    QFontComboBox *fontComboBox;
    QLabel *fontLabel2;
    QComboBox *sizeComboBox;
    QToolButton *boldBtn;
    QToolButton *italicBtn;
    QToolButton *underlineBtn;
    QToolButton *colorBtn; QToolBar *fontToolBar; //字体工具栏 QLabel *listLabel; //排序设置项
    QComboBox *listComboBox;
    QActionGroup *actGrp;
    QAction *leftAction;
    QAction *rightAction;
    QAction *centerAction;
    QAction *justifyAction; QToolBar *listToolBar; //排序工具栏
    private slots:
    void file_open();
    void file_close();
    void file_save();
    void file_exit();
    void file_selall();
    void file_cut();
    void file_copy();
    void file_paste();
    void build_it();
    void build_andrun();
    void about_it(); }; #endif // MAINWINDOW_H
  • mainwindow.cpp
     #include "mainwindow.h"
    #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    { ui->setupUi(this);
    //设置文本编辑器
    text = new QTextEdit;
    QFont font;
    font.setPixelSize();
    text->setFont(font);
    QColor qcolor;
    qcolor.setRgb(,,);
    this->setCentralWidget(text); //设置菜单
    file = this->menuBar()->addMenu("文件");
    edit = this->menuBar()->addMenu("编辑");
    build = this->menuBar()->addMenu("构建");
    about = this->menuBar()->addMenu("关于"); //about菜单
    {
    Aabout_it = new QAction("关于",this);
    about->addAction(Aabout_it);
    connect(Aabout_it,SIGNAL(triggered(bool)),this,SLOT(about_it()));
    } //open菜单
    {
    Afile_open = new QAction("打开文件",this);
    file->addAction(Afile_open);
    connect(Afile_open,SIGNAL(triggered(bool)),this,SLOT(file_open())); Afile_close = new QAction("关闭文件",this);
    file->addAction(Afile_close);
    connect(Afile_close,SIGNAL(triggered(bool)),this,SLOT(file_close())); Afile_save = new QAction("保存文件",this);
    file->addAction(Afile_save);
    connect( Afile_save,SIGNAL(triggered(bool)),this,SLOT(file_save())); Afile_exit = new QAction("退出",this);
    file->addAction(Afile_exit);
    connect(Afile_exit,SIGNAL(triggered(bool)),this,SLOT(file_exit()));
    } //编辑菜单
    {
    Afile_selall = new QAction("全选",this);
    edit->addAction(Afile_selall);
    connect(Afile_selall,SIGNAL(triggered(bool)),this,SLOT(file_selall())); Afile_cut = new QAction("剪切",this);
    edit->addAction(Afile_cut);
    connect(Afile_cut,SIGNAL(triggered(bool)),this,SLOT(file_cut())); Afile_copy = new QAction("复制",this);
    edit->addAction(Afile_copy);
    connect(Afile_copy,SIGNAL(triggered(bool)),this,SLOT(file_copy())); Afile_paste = new QAction("粘贴",this);
    edit->addAction(Afile_paste);
    connect(Afile_paste,SIGNAL(triggered(bool)),this,SLOT(file_paste()));
    } {
    Abuild_it = new QAction("编译",this);
    build->addAction(Abuild_it);
    connect(Abuild_it,SIGNAL(triggered(bool)),this,SLOT(build_it())); Abuild_andrun = new QAction("编译执行",this);
    build->addAction(Abuild_andrun);
    connect(Abuild_andrun,SIGNAL(triggered(bool)),this,SLOT(build_andrun()));
    } this->setWindowTitle("简易编译器"); //showWidget =new ShowWidget(this);
    // setCentralWidget(showWidget);
    //在工具栏上嵌入控件
    //设置字体
    fontLabel1 =new QLabel(tr("字体:"));
    fontComboBox =new QFontComboBox;
    fontComboBox->setFontFilters(QFontComboBox::ScalableFonts); fontLabel2 =new QLabel(tr("字号:"));
    sizeComboBox =new QComboBox;
    QFontDatabase db;
    foreach(int size,db.standardSizes())
    sizeComboBox->addItem(QString::number(size)); boldBtn =new QToolButton;
    boldBtn->setIcon(QIcon("bold.png"));
    boldBtn->setCheckable(true);
    italicBtn =new QToolButton;
    italicBtn->setIcon(QIcon("italic.png"));
    italicBtn->setCheckable(true); underlineBtn =new QToolButton;
    underlineBtn->setIcon(QIcon("underline.png"));
    underlineBtn->setCheckable(true); colorBtn =new QToolButton;
    colorBtn->setIcon(QIcon("color.png"));
    colorBtn->setCheckable(true); //排序
    listLabel =new QLabel(tr("排序"));
    listComboBox =new QComboBox;
    listComboBox->addItem("Standard");
    listComboBox->addItem("QTextListFormat::ListDisc");
    listComboBox->addItem("QTextListFormat::ListCircle");
    listComboBox->addItem("QTextListFormat::ListSquare");
    listComboBox->addItem("QTextListFormat::ListDecimal");
    listComboBox->addItem("QTextListFormat::ListLowerAlpha");
    listComboBox->addItem("QTextListFormat::ListUpperAlpha");
    listComboBox->addItem("QTextListFormat::ListLowerRoman");
    listComboBox->addItem("QTextListFormat::ListUpperRoman"); createActions();
    createMenus();
    createToolBars(); if(img.load("image.png"))
    {
    //showWidget->imageLabel->setPixmap(QPixmap::fromImage(img));
    } } MainWindow::~MainWindow()
    {
    delete ui;
    } void MainWindow::file_open()
    {
    filename =QFileDialog::getOpenFileName(this,"hello","*.*");
    if(filename.isEmpty())
    {
    QMessageBox::information(this,"提示","注意选择一个文件");
    return;
    }
    fstream fin(filename.toStdString().c_str());
    if(!fin)
    {
    QMessageBox::information(this,"提示","打开文件失败");
    return;
    } while(!fin.eof())
    {
    char buf[]={};
    fin.getline(buf,);
    text->append(buf);
    }
    fin.close();
    } void MainWindow::file_close()
    {
    text->setText("");
    filename ="";
    } void MainWindow::file_save()
    {
    filename = QFileDialog::getSaveFileName();
    if(filename.isEmpty())
    {
    QMessageBox::information(this,"提示","打开文件失败");
    return;
    } QString info =text->toPlainText().toStdString().c_str();
    //写入
    ofstream fout(filename.toStdString().c_str());
    if(!fout)
    {
    QMessageBox::information(this,"提示","打开文件失败");
    return;
    }
    fout.write(info.toStdString().c_str(),strlen(info.toStdString().c_str()));
    fout.close();
    } void MainWindow::file_exit()
    {
    exit();
    } void MainWindow::file_selall()
    {
    text->selectAll();
    } void MainWindow::file_cut()
    {
    text->cut();
    } void MainWindow::file_copy()
    {
    text->copy();
    } void MainWindow::file_paste()
    {
    text->paste();
    } void MainWindow::build_it()
    {
    QString destexename = filename;
    bool iscpp = destexename.contains(".cpp");
    QString cmd;
    if(iscpp)
    {
    destexename.replace(".cpp",".exe");
    cmd = "g++ -o " + destexename+" " +filename;
    }
    else
    {
    destexename.replace(".c",".exe");
    cmd = "gcc -o "+destexename+" "+filename;
    }
    system(cmd.toStdString().c_str());
    } void MainWindow::build_andrun()
    {
    QString destexename = filename;
    bool iscpp = destexename.contains(".cpp");
    QString cmd;
    if(iscpp)
    {
    destexename.replace(".cpp",".exe");
    cmd = "g++ -o "+filename+" "+destexename;
    }
    else
    {
    destexename.replace(".c",".exe");
    cmd = "gcc -o "+filename+" "+destexename;
    }
    system(cmd.toStdString().c_str()); system(destexename.toStdString().c_str());
    } void MainWindow::about_it()
    {
    QMessageBox::information(this,"hello","版权所有,盗版不究");
    } void MainWindow::createActions()
    {
    //"打开"动作
    openFileAction =new QAction(QIcon("open.png"),tr("打开"),this);
    openFileAction->setShortcut(tr("Ctrl+O"));
    openFileAction->setStatusTip(tr("打开一个文件"));
    // connect(openFileAction,SIGNAL(triggered()),this,SLOT(ShowOpenFile())); //"新建"动作
    NewFileAction =new QAction(QIcon("new.png"),tr("新建"),this);
    NewFileAction->setShortcut(tr("Ctrl+N"));
    NewFileAction->setStatusTip(tr("新建一个文件"));
    //connect(NewFileAction,SIGNAL(triggered()),this,SLOT(ShowNewFile())); //"退出"动作
    exitAction =new QAction(tr("退出"),this);
    exitAction->setShortcut(tr("Ctrl+Q"));
    exitAction->setStatusTip(tr("退出程序"));
    //connect(exitAction,SIGNAL(triggered()),this,SLOT(close())); //"复制"动作
    copyAction =new QAction(QIcon("copy.png"),tr("复制"),this);
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip(tr("复制文件"));
    // connect(copyAction,SIGNAL(triggered()),showWidget->text,SLOT(copy())); //"剪切"动作
    cutAction =new QAction(QIcon("cut.png"),tr("剪切"),this);
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("剪切文件"));
    //connect(cutAction,SIGNAL(triggered()),showWidget->text,SLOT(cut())); //"粘贴"动作
    pasteAction =new QAction(QIcon("paste.png"),tr("粘贴"),this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("粘贴文件"));
    // connect(pasteAction,SIGNAL(triggered()),showWidget->text,SLOT(paste())); //"关于"动作
    aboutAction =new QAction(tr("关于"),this);
    // connect(aboutAction,SIGNAL(triggered()),this,SLOT(QApplication::aboutQt())); //"打印文本"动作
    PrintTextAction =new QAction(QIcon("printText.png"),tr("打印文本"), this);
    PrintTextAction->setStatusTip(tr("打印一个文本"));
    // connect(PrintTextAction,SIGNAL(triggered()),this,SLOT(ShowPrintText())); //"打印图像"动作
    PrintImageAction =new QAction(QIcon("printImage.png"),tr("打印图像"), this);
    PrintImageAction->setStatusTip(tr("打印一幅图像"));
    // connect(PrintImageAction,SIGNAL(triggered()),this,SLOT(ShowPrintImage())); //"放大"动作
    zoomInAction =new QAction(QIcon("zoomin.png"),tr("放大"),this);
    zoomInAction->setStatusTip(tr("放大一张图片"));
    // connect(zoomInAction,SIGNAL(triggered()),this,SLOT(ShowZoomIn())); //"缩小"动作
    zoomOutAction =new QAction(QIcon("zoomout.png"),tr("缩小"),this);
    zoomOutAction->setStatusTip(tr("缩小一张图片"));
    // connect(zoomOutAction,SIGNAL(triggered()),this,SLOT(ShowZoomOut())); //实现图像旋转的动作(Action)
    //旋转90°
    rotate90Action =new QAction(QIcon("rotate90.png"),tr("旋转90°"),this);
    rotate90Action->setStatusTip(tr("将一幅图旋转90°"));
    // connect(rotate90Action,SIGNAL(triggered()),this,SLOT(ShowRotate90())); //旋转180°
    rotate180Action =new QAction(QIcon("rotate180.png"),tr("旋转180°"), this);
    rotate180Action->setStatusTip(tr("将一幅图旋转180°"));
    connect(rotate180Action,SIGNAL(triggered()),this,SLOT(ShowRotate180())); //旋转270°
    rotate270Action =new QAction(QIcon("rotate270.png"),tr("旋转270°"), this);
    rotate270Action->setStatusTip(tr("将一幅图旋转270°"));
    // connect(rotate270Action,SIGNAL(triggered()),this,SLOT(ShowRotate270())); //实现图像镜像的动作(Action)
    //纵向镜像
    mirrorVerticalAction =new QAction(tr ("纵向镜像"),this);
    mirrorVerticalAction->setStatusTip(tr("对一张图作纵向镜像"));
    // connect(mirrorVerticalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorVertical())); //横向镜像
    mirrorHorizontalAction =new QAction(tr("横向镜像"),this);
    mirrorHorizontalAction->setStatusTip(tr("对一张图作横向镜像"));
    //connect(mirrorHorizontalAction,SIGNAL(triggered()),this,SLOT(ShowMirrorHorizontal())); //排序:左对齐、右对齐、居中和两端对齐
    actGrp =new QActionGroup(this); leftAction =new QAction(QIcon("left.png"),"左对齐",actGrp);
    leftAction->setCheckable(true); rightAction =new QAction(QIcon("right.png"),"右对齐",actGrp);
    rightAction->setCheckable(true); centerAction =new QAction(QIcon("center.png"),"居中",actGrp);
    centerAction->setCheckable(true); justifyAction =new QAction(QIcon("justify.png"),"两端对齐",actGrp);
    justifyAction->setCheckable(true); // connect(actGrp,SIGNAL(triggered(QAction*)),this,SLOT(ShowAlignment(QAction*))); //实现撤销和重做的动作(Action)
    //撤销和重做
    undoAction =new QAction(QIcon("undo.png"),"撤销",this);
    // connect(undoAction,SIGNAL(triggered()),showWidget->text,SLOT(undo()));
    redoAction =new QAction(QIcon("redo.png"),"重做",this);
    //connect(redoAction,SIGNAL(triggered()),showWidget->text,SLOT(redo()));
    } void MainWindow::createMenus()
    {
    //文件菜单
    fileMenu =menuBar()->addMenu(tr("文件"));
    fileMenu->addAction(openFileAction);
    fileMenu->addAction(NewFileAction);
    fileMenu->addAction(PrintTextAction);
    fileMenu->addAction(PrintImageAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction); //缩放菜单
    zoomMenu =menuBar()->addMenu(tr("编辑"));
    zoomMenu->addAction(copyAction);
    zoomMenu->addAction(cutAction);
    zoomMenu->addAction(pasteAction);
    zoomMenu->addAction(aboutAction);
    zoomMenu->addSeparator();
    zoomMenu->addAction(zoomInAction);
    zoomMenu->addAction(zoomOutAction); //旋转菜单
    rotateMenu =menuBar()->addMenu(tr("旋转"));
    rotateMenu->addAction(rotate90Action);
    rotateMenu->addAction(rotate180Action);
    rotateMenu->addAction(rotate270Action); //镜像菜单
    mirrorMenu =menuBar()->addMenu(tr("镜像"));
    mirrorMenu->addAction(mirrorVerticalAction);
    mirrorMenu->addAction(mirrorHorizontalAction);
    } void MainWindow::createToolBars()
    {
    //文件工具条
    fileTool =addToolBar("File");
    fileTool->addAction(openFileAction);
    fileTool->addAction(NewFileAction);
    fileTool->addAction(PrintTextAction);
    fileTool->addAction(PrintImageAction); //编辑工具条
    zoomTool =addToolBar("Edit");
    zoomTool->addAction(copyAction);
    zoomTool->addAction(cutAction);
    zoomTool->addAction(pasteAction);
    zoomTool->addSeparator();
    zoomTool->addAction(zoomInAction);
    zoomTool->addAction(zoomOutAction); //旋转工具条
    rotateTool =addToolBar("rotate");
    rotateTool->addAction(rotate90Action);
    rotateTool->addAction(rotate180Action);
    rotateTool->addAction(rotate270Action); //撤销和重做工具条
    doToolBar =addToolBar("doEdit");
    doToolBar->addAction(undoAction);
    doToolBar->addAction(redoAction); //字体工具条
    fontToolBar =addToolBar("Font");
    fontToolBar->addWidget(fontLabel1);
    fontToolBar->addWidget(fontComboBox);
    fontToolBar->addWidget(fontLabel2);
    fontToolBar->addWidget(sizeComboBox);
    fontToolBar->addSeparator();
    fontToolBar->addWidget(boldBtn);
    fontToolBar->addWidget(italicBtn);
    fontToolBar->addWidget(underlineBtn);
    fontToolBar->addSeparator();
    fontToolBar->addWidget(colorBtn); //排序工具条
    listToolBar =addToolBar("list");
    listToolBar->addWidget(listLabel);
    listToolBar->addWidget(listComboBox);
    listToolBar->addSeparator();
    listToolBar->addActions(actGrp->actions());
    }
  • main.cpp
     #include "mainwindow.h"
    #include <QApplication> int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    w.resize(,);
    return a.exec();
    }

30.QT IDE编写的更多相关文章

  1. 利用Eric+Qt Designer编写倒计时时钟

    [前言]前几日通过编写命令行通讯录,掌握了Python的基本语法结构,于是开始向更高水平冲击,利用Eric与Qt Designer 编写一个带界面的小程序.本次实操中也确实遇到了不少问题,通过学习也都 ...

  2. 用Sublime Text搭建简易IDE编写Verilog代码

    前言 Verilog是一种硬件描述语言(HDL),该语言在Windows上有集成开发环境可以使用,如ModelSim,但ModelSim的编辑器不太好用因此笔者萌生了用Sublime Text3来编写 ...

  3. 利用IDE编写C语言程序的一点注意事项

    前言:我是喜欢编程的一只菜鸟,在自学过程中,对遇到的一些问题和困惑,有时虽有一点体会感悟,但时间一长就会淡忘,很不利于知识的积累.因此,想通过博客园这个平台,一来记录自己的学习体会,二来便于向众多高手 ...

  4. Qt+QZXing编写识别二维码的程序

    本人最近在用Qt编写程序,需要用编写二维码识别功能.在网上搜寻一番,找到了QZXing.配置过程中确实出了一大把汗,这里我写这篇文章记录配置方法,替后人省一把汗吧!我的开发环境:MSVC2010 + ...

  5. VLC和Qt结合编写流媒体rtsp播放器

            VLC播放器是一款功能强大且小巧的播放器,它支持多种多样的音视频格式,比如MPEG1,2以及mp3等等.并且通过Qt和VLC的结合能让每个开发者写出自己的视频流媒体播放器.     Q ...

  6. Qt *.pro编写一般规则

    qmake 之 CONFIG 与 QT 乱谈 看qtcn论坛中经常有人忘记 QT+=network 等语句.随便写写吧,或许对他人有帮助. 写来写去,发现越写越乱,就这样吧,反正主要的内容很简单. d ...

  7. Qt+imx6编写的楼宇对讲管理平台

    第一个初步版本. 1:楼宇对讲模块.住户报警模块.门禁控制模块.系统设置模块. 2:实时对讲信息卡片式展示,通话记录表格展示. 3:设备面板展示,实时显示上下线报警等信息. 4:设备查询.记录查询.运 ...

  8. windows下Qt Creator5.1.0编写程序以及调用OpenCV库

    系统说明 最近使用opencv编写程序,程序编的差不多就学习使用QT加个界面,首先声明下本人的系统和使用的软件版本, 系统: windows xp QT IDE:QT Creator5.1.0 Ope ...

  9. 编写第一个Qt程序

    http://c.biancheng.net/view/1817.html 学习一种编程语言或编程环境,通常会先编写一个“Hello World”程序.我们也用 Qt Creator 编写一个“Hel ...

随机推荐

  1. 原型模式(Prototype)C++实现

    意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 实用性:1.当要实例化的类是在运行时刻指定时. 2.为了避免创建一个与产品类层次平行的工厂类层次时. 3.当一个类的实例只能有几 ...

  2. 使用jQuery对图片进行居中设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. RAP开发入门-布局管理

    布局类继承关系 FillLayout  new FillLayout(SWT.VERTICAL/HORIZONTAL)设置竖直/水平填充 RowLayout wrap折行显示.pack自适应布局(布局 ...

  4. PostgreSQL的HA解决方案-2负载均衡(load balance)

    一.部署说明 1.1 实施环境 本文档实验环境如下: PGSQL主机: 192.168.1.45 PGSQL备机: 192.168.1.50 软件和系统版本 Pgsql 版本: pgsql 9.2.4 ...

  5. 生物信息之ME, HMM, MEMM, CRF

    原文链接:http://bbs.sciencenet.cn/home.php?mod=space&uid=260809&do=blog&id=573755 注:有少量修改!如有 ...

  6. 优动漫PAINT核心功能介绍

    优动漫PAINT是一款功能强大的动漫绘图软件,适用于个人和专业团队创作,分为个人版和EX版.搭载了绘制漫画和插画所需的所有功能——丰富的笔工具.超强的笔压感应和手颤修正功能,可分别满足画师对于插画.漫 ...

  7. MongoDB 学习笔记(七):主从复制与副本集

    一.主从复制 1.主从复制是一个简单的数据库同步备份的集群技术,如下图:要明确的知道主服务器与从服务器,且从服务器要明确的知道主服务器的存在. 2.在MongoDB中在启动数据库服务时,可以用mast ...

  8. Here comes Treble: A modular base for Android

    On the Android team, we view each dessert release as an opportunity to make Android better for our u ...

  9. 理解Faster-RCNN 中的Anchor

    先上图看一下Faster R-CNN操作流程:  图片说明:Faster R-CNN=Fast R-CNN+RPN,其中Fast R-CNN结构不变:RPN负责生成proposals,配合最后一层的f ...

  10. GDI 线段绘制示例程序

    #include <windows.h> #include <strsafe.h> #include <stack> typedef struct tagLINE ...