Qt实现自己的IDE

简易实现了文件操作,编译和运行等功能:

mainwindow.h文件代码:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

//add控件
#include<QTextEdit>//Qt自带的控件
#include<QMenu>
#include<QMenuBar>
#include<QAction>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QString filename;
    QString savefilename;


private :
    QTextEdit *text1;
    QMenu *file;
    QMenu *edit;
    QMenu *build;
    QMenu *help;




    QAction *file_open;
    QAction *file_exit;
    QAction *help_about;

    QAction *edit_copy;
    QAction *edit_cut;
    QAction *edit_paste;

    QAction *select_all;
    QAction *file_save;

    QAction *build_compile;

    QAction *build_run;

private slots:
    void on_open();
    void on_about();
    void on_exit();
    void on_copy();
    void on_cut();
    void on_paste();
    void on_selectall();
    void on_save();

    void on_compile();
    void on_run();
};

#endif // MAINWINDOW_H

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();
}

mainwindow.cpp文件代码:

#include "mainwindow.h"
#include "QMessageBox"
#include "QFileDialog"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    text1=new QTextEdit;
    QFont f;
    f.setPixelSize(24);
    text1->setFont(f);//设置字体的大小

    QColor c;
    c.setRgb(160,40,120);
    text1->setTextColor(c);//设置字体的颜色

    this->setCentralWidget(text1);//将这个控件放到对话框的中间

    file=this->menuBar()->addMenu("文件");//在菜单栏中添加菜单项
    edit=this->menuBar()->addMenu("编辑");
    build=this->menuBar()->addMenu("构建");
    help=this->menuBar()->addMenu("帮助");

    file_open=new QAction("打开",this);//建立一个action
    file_open->setShortcut(tr("Ctrl+O"));//添加 快捷键
    file->addAction(file_open);//将file_open这个action加入到file菜单中

    file_save=new QAction("保存",this);
    file_save->setShortcut(tr("Ctrl+S"));
    file->addAction(file_save);

    file_exit=new QAction("tuichu",this);

    file->addSeparator();//添加分隔符
    file->addAction(file_exit);

    help_about=new QAction("关于",this);
    help_about->setShortcut(tr("Ctrl+H"));
    help->addAction(help_about);

    edit_copy=new QAction("拷贝",this);
    edit->addAction(edit_copy);

    edit_cut=new QAction("剪切",this);
    edit->addAction(edit_cut);

    edit_paste=new QAction("粘贴",this);
    edit->addAction(edit_paste);

    select_all=new QAction("select all",this);
    edit->addAction(select_all);

    build_compile=new QAction("编译",this);
    build->addAction(build_compile);

    build_run=new QAction("运行",this);
    build->addAction(build_run);



    //Qt的消息槽机制
    connect(file_open,SIGNAL(triggered()),this,SLOT(on_open()));
    //第一个参数是触发事件的控件,第二个参数是对于Action来说的固定写法
    //第三个参数固定写this,第四个参数指定点击Action后执行那个函数

    connect(help_about,SIGNAL(triggered()),this,SLOT(on_about()));
    connect(file_exit,SIGNAL(triggered()),this,SLOT(on_exit()));
    connect(edit_copy,SIGNAL(triggered()),this,SLOT(on_copy()));
    connect(edit_cut,SIGNAL(triggered()),this,SLOT(on_cut()));
    connect(edit_paste,SIGNAL(triggered()),this,SLOT(on_paste()));
    connect(select_all,SIGNAL(triggered()),this,SLOT(on_selectall()));
    connect(file_save,SIGNAL(triggered()),this,SLOT(on_save()));

    connect(build_compile,SIGNAL(triggered()),this,SLOT(on_compile()));
    connect(build_run,SIGNAL(triggered()),this,SLOT(on_run()));
}

MainWindow::~MainWindow()
{
    delete text1;
}

void MainWindow::on_open()
{
    filename=QFileDialog::getOpenFileName();
    //函数返回路径+文件名

    if(filename.isEmpty())
    {
        return;
    }

    QString content;//QString 是Qt封装的一个字符串类
    //QMessageBox::information(this,"提示","test");

    //filename.toStdString().data();//这个语句即可将QString类型转化为const char *类型
    FILE *p=fopen(filename.toStdString().data(),"r");//需要将QString转换为const char *
    if(p==NULL)
    {
        QMessageBox::information(this,"错误","打开文件失败");
    }
    else
    {
        while(!feof(p))
        {
            char buf[1024]={0};
            fgets(buf,sizeof(buf),p);
            content+=buf;//将读取到的内容追加到content后面
        }
      fclose(p);
      text1->setText(content);//将字符串的值放到text里面
    }


}
void MainWindow::on_about()
{
    QMessageBox::information(this,"帮助","Jumfens\n2010-2016");
}

void MainWindow::on_exit()
{
    exit(0);
}

void MainWindow::on_copy()
{
    text1->copy();
}

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

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

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

void MainWindow::on_save()
{
     savefilename=QFileDialog::getSaveFileName();
     if(savefilename==NULL)
     {
         return;
     }
     FILE *p=fopen(savefilename.toStdString().data(),"w");
     if(p==NULL)
     {
         QMessageBox::information(this,"错误","打开文件失败");
         return;
     }
     else
     {
        // text1->toPlainText().toStdString().data();//将用户在控件中输入的字符串转化为const char *
         fputs(text1->toPlainText().toStdString().data(),p);
         fclose(p);
     }


}

void MainWindow::on_compile()
{
    QString content="#include<stdio.h>\n";
    FILE *p=fopen(filename.toStdString().data(),"r");
    if(p==NULL)
        return;
    while(!feof(p))
    {
        char buf[1024]={0};
        fgets(buf,sizeof(buf),p);
        content+=buf;
    }
    fclose(p);

    content.replace("111","int").replace("222","main").replace("333","{").replace("444","printf(\"hello jumfens\")").replace("8888","printf(\"\\n\");").replace("555","getchar();").replace("666","return").replace("777","}");

    QMessageBox::information(this,"显示",content);
    //    return ;

    QString destfilename=filename;
    destfilename.replace(".e",".c");
    p=fopen(destfilename.toStdString().data(),"w");
    fputs(content.toStdString().data(),p);
    fclose(p);

    QString srcname=destfilename;
    srcname.replace(".c",".exe");


    QString command="gcc -o "+srcname +" "+destfilename;
    system(command.toStdString().data());

    // remove(destfilename.toStdString().data());
    //可以将中间生成的.c文件删除

}

void MainWindow::on_run()
{
    QString destfilename=filename;
    destfilename.replace(".e",".exe");
    system(destfilename.toStdString().data());
}
可以输入自己风格的编程语言,然后保存,打开后可以编译生成.c文件,还可以生成.exe文件,最后运行文件。

  • 2
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值