• 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. c++中的强制转换

    一.C语言的强制转换1.1 隐性转换 不同数据类型之间赋值和运算,函数调用传递参数等等,由编译器完成        int        nTmp = 10;        short    sTmp ...

  2. 常用的CSS命名

    头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体佈局宽度:wrapper 左右中:left rig ...

  3. 5) 十分钟学会android--ActionBar知识串烧

    建立ActionBar Action bar 最基本的形式,就是为 Activity 显示标题,并且在标题左边显示一个 app icon.即使在这样简单的形式下,action bar对于所有的 act ...

  4. [原创]Eclipse 安卓开发几个异常的处理办法

    一.代码没有问题,就是报错,重启一下就会好.可以先clean再build; 二.R.Java丢失 网上讲了若干方法,有用android toos的,有clean再build的,我的解决办法是勾选bui ...

  5. hdu3926 Hand in Hand 判断同构

    因为每个人小朋友只有两只手,所以每个点最多只有2度.图有可能是环.链,以及环和链构成的复杂图. 如何判断两幅图是否相似呢?判断相似是判断两幅图的圈的数量,以及构成圈的点数是否相同.还有判断链的数目和构 ...

  6. open source project for recommendation system

    原文链接:http://blog.csdn.net/cserchen/article/details/14231153 目前互联网上所能找到的知名开源推荐系统(open source project ...

  7. Vtk读取并显示保存图像

    (1):Vtk读取并显示图像:三种方法 转载:未知出处 转载:用VTK显示平面图片:http://blog.csdn.net/tonylk/article/details/464881 用到vtkJP ...

  8. PuTTY 命令行改进 有效解决 中文乱码

    PuTTY  是一个免费且跨平台的并支持SSH和Telnet 的客户端, 包括xterm 终端模拟器. 它由Simon Tatham 编写并维护. http://www.chiark.greenend ...

  9. 企业级任务调度框架Quartz(6) 任务调度器(Scheduler)

    前序:      我们已经在前面的内容能里看到了,我们用 Scheduler 来管理我们的 Job:创建并关联触发器以使 Job 能被触发执行:以及如可选择 calendar 为给定的时程安排提供更多 ...

  10. day22 包,相对/绝对路径

    目录 包 包被导入时发生的三件事 为什么要有包 相对路径 绝对路径 包 包是一个文件夹,也是一个模块,只是为了区分单个文件的模块,称之为包.因为单纯的文件夹无法作为模块,文件夹内的__init__.p ...