QT 自己制作IDE工具

发布时间:2020-06-22 15:54:09 作者:990487026
来源:网络 阅读:1035



项目创建

QT 自己制作IDE工具


基类选择

QT 自己制作IDE工具




项目文件

QT 自己制作IDE工具





编译运行


QT 自己制作IDE工具




QT 自己制作IDE工具


QT 自己制作IDE工具



项目文件1 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextEdit>    //文本
#include <QMenu>        //加入菜单
#include <QMenuBar>     //加入菜单栏
#include <QAction>      //加入菜单栏
#include <QFileDialog>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
     MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    bool is_changed;
    QString  filename;   //当前文件的名字
    QTextEdit *text1;   //中央的TextEdit控件

    //文件菜单
    QMenu *file_menu;   //文件菜单
    QAction *new_file;   //新建文件菜单项
    QAction *open_file;   //打开文件菜单项
    QAction *save_file;   //保存文件菜单项
    QAction *exit_file;   //退出文件菜单项

    //编辑菜单
    QMenu *edit_menu;   //编辑菜单
    QAction *copy_edit;   //编辑菜单的复制按钮
    QAction *paste_edit;
    QAction *cut_edit;
    QAction *allselect_edit;

    QMenu *help_menu;   //帮助菜单

    //编译菜单
    QMenu *comp_menu;   //编译菜单
    QAction *comp_comp;   //编译按钮
    QAction *run_comp;   //运行按钮


    void precomp();
private slots:
    void on_exit();//在QT编辑环境,安装ALT+ENTER,出现提示再按一次回车
    void on_open();
    void on_save();
    void on_new();
    void on_copy();
    void on_paste();
    void on_cut();
    void on_allselect();
    void on_changed();
    void on_comp();
    void on_run();

};

#endif // MAINWINDOW_H



项目文件2  main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.resize(800,600);//设置主窗口的长宽
    w.show();

    return a.exec();
}




项目文件3 mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)//构造函数
    : QMainWindow(parent)
{
    text1 = new QTextEdit;          //在堆中创建一个QTextEdit实例
    text1->setFontPointSize(14);    //设置窗体的字体大小
    this->setCentralWidget(text1);  //将TextEdit控件加入到主窗口的工作区


    //文本内容在保存前是否发生变动
    is_changed = false;

    //添加菜单项,并为其设定快捷键
    //【文件菜单栏】
    file_menu = this->menuBar()->addMenu("文件");
    new_file = new QAction("新建文件",this);  //第一个参数是菜单项的文字,第二个参数是指向主体的指针
    new_file ->setShortcut(tr("ctrl+n"));   //设定快捷键
    file_menu ->addAction(new_file);        //关联 菜单栏 与 菜单项
    file_menu ->addSeparator();             //在文件下拉菜单上面显示一个分隔符

    open_file = new QAction("打开文件",this);
    file_menu ->addAction(open_file);
    open_file ->setShortcut(tr("ctrl+o"));

    save_file = new QAction("保存文件",this);
    file_menu ->addAction(save_file);
    save_file ->setShortcut(tr("ctrl+s"));

    exit_file = new QAction("退出",this);
    file_menu ->addAction(exit_file);


    //【编辑菜单栏】
    edit_menu = this->menuBar()->addMenu("编辑");
    copy_edit = new QAction("复制",this);
    copy_edit ->setShortcut(tr("ctrl+c"));
    edit_menu ->addAction(copy_edit);

    paste_edit = new QAction("粘贴",this);
    paste_edit ->setShortcut(tr("ctrl+v"));
     edit_menu ->addAction(paste_edit);

    cut_edit = new QAction("剪切",this);
    cut_edit ->setShortcut(tr("ctrl+x"));
    edit_menu ->addAction(cut_edit);

    allselect_edit = new QAction("全选",this);
    allselect_edit ->setShortcut(tr("ctrl+a"));
    edit_menu ->addAction(allselect_edit);

    //【编译菜单栏】
    comp_menu = this->menuBar()->addMenu("编译");
    comp_comp = new QAction("编译",this); comp_menu->addAction(comp_comp);
    run_comp = new QAction("运行",this);  comp_menu->addAction(run_comp);

    //【帮助菜单栏】
    help_menu = this->menuBar()->addMenu("帮助");



    //【鼠标事件与函数关联】当鼠标点击exit_file 菜单的时候,执行on_exit()函数
    connect(exit_file,SIGNAL(triggered()),this,SLOT(on_exit()));
    connect(open_file,SIGNAL(triggered()),this,SLOT(on_open()));
    connect(save_file,SIGNAL(triggered()),this,SLOT(on_save()));
    connect(new_file,SIGNAL(triggered()),this,SLOT(on_new()));
    connect(copy_edit,SIGNAL(triggered()),this,SLOT(on_copy()));
    connect(paste_edit,SIGNAL(triggered()),this,SLOT(on_paste()));
    connect(cut_edit,SIGNAL(triggered()),this,SLOT(on_cut()));
    connect(allselect_edit,SIGNAL(triggered()),this,SLOT(on_allselect()));
    connect(text1,SIGNAL(textChanged()),this,SLOT(on_changed()));//当文本内容发生变化时,触发on_changed函数
    connect(comp_comp,SIGNAL(triggered()),this,SLOT(on_comp()));//当文本内容发生变化时,触发on_changed函数
    connect(run_comp,SIGNAL(triggered()),this,SLOT(on_run()));//当文本内容发生变化时,触发on_changed函数

}

MainWindow::~MainWindow()//析构函数
{
    delete text1;
    text1 = NULL;
}

void MainWindow::precomp()//预编译
{
    FILE *p = fopen(filename.toStdString().data(),"r");
    if(p == NULL) return ;
    QString cmd = filename +".c";
    FILE *p1 = fopen(cmd.toStdString().data(),"w");
    if(p1 == NULL) return ;
    QString str;
    while(!feof(p))
    {
        char buf[1024] = {0};
        fgets(buf,sizeof(buf),p);
        str += buf;
    }

    str.replace("包含","#include");
    str.replace("主函数","main");
    str.replace("整数","int");
    str.replace("开始","{");
    str.replace("收工","}");
    str.replace("。",";");
    str.replace("返回","return");
    str.replace("打印","printf");
    str.replace("输入输出","<stdio.h>");
    str.replace("无声的等待...","getchar()");

    fputs(str.toStdString().data(),p1);
    fclose(p);
    fclose(p1);
}

//程序退出
void MainWindow::on_exit()
{
    this ->close();
}
//打开文件
void MainWindow::on_open()
{
   filename =  QFileDialog::getOpenFileName(this,"打开");
   if(filename.isEmpty()) return ;//考虑用户选择取消的情景
   FILE *p = fopen(filename.toStdString().data(),"r");
   if(p == NULL) return ;
    QString str;
    while(!feof(p))
    {
        char buf[1024] = {0};
        fgets(buf,sizeof(buf),p);
        str += buf;
    }
    fclose(p);
    text1->setText(str);
    is_changed = false;
}
//保存文件
void MainWindow::on_save()
{
    if(filename.isEmpty())
    {
        filename = QFileDialog::getSaveFileName(this,"保存文件");
    }
    if(!filename.isEmpty())
    {
        FILE *p = fopen(filename.toStdString().data(),"w");
        if(p == NULL) return ;
        QString str = text1->toPlainText();
        fputs(str.toStdString().data(),p);
        fclose(p);
    }
}
//新建文件
void MainWindow::on_new()
{
    if(is_changed == true)
    {
        on_save();
        is_changed = false;
    }
    filename = "";
    text1->setText("");
}
//IDE的复制功能
void MainWindow::on_copy()
{
    text1->copy();
}

void MainWindow::on_paste()
{
    text1->paste();
}

void MainWindow::on_cut()
{
    text1->cut();
}

void MainWindow::on_allselect()
{
    text1->selectAll();
}

void MainWindow::on_changed()
{
    is_changed = true;
}
//编译并运行按钮
void MainWindow::on_comp()
{
    if (is_changed == true)//在点击编译按钮,如果文本内容发生变化,就自动保存
    {
        on_save();
    }
    precomp();//自动以预编译
    QString cmd;
    const char *s = filename.toStdString().data();
    cmd.sprintf("gcc -o %s.exe %s.c",s,s);
    system(cmd.toStdString().data());//先编译

    //如何删除那个临时文件呢
    cmd = filename.replace("/","\\") + ".c";
    remove(cmd.toStdString().data());


    cmd = filename + ".exe";
    system(cmd.toStdString().data());//再运行
}

void MainWindow::on_run()
{
    QString cmd;
    cmd = filename + ".exe";
    system(cmd.toStdString().data());
}



推荐阅读:
  1. C++ 到 Qt编译过程方法
  2. QT应用程序图标

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

制作 自己 qt

上一篇:html中实现字体加粗的方法

下一篇:三层交换机实现不同VLAN的互通

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》