欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c语言入门:用qt实现简单IDE

程序员文章站 2022-05-09 18:29:33
c语言入门:用qt实现简单IDE main.cpp #include "mainwindow.h" #include int main(int...

c语言入门:用qt实现简单IDE

main.cpp

#include "mainwindow.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.resize(1000,800);
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    QString file_path;
    QTextEdit *te;
    QMenu *file;
    QMenu *edit;
    QMenu *build;
    QAction *open;
    QAction *save;
    QAction *exit_;

    QAction *copy;
    QAction *paste;
    QAction *cut;

    QAction *buildc;
    QAction *run;

    ~MainWindow();

private:
    Ui::MainWindow *ui;

private slots:
    void on_open_click();
    void on_save_click();
    void on_exit_click();

    void on_copy_click();
    void on_paste_click();
    void on_cut_click();

    void on_buildc_click();
    void on_run_click();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
//#include <>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    te=new QTextEdit;
    QFont font;
    font.setPointSize(10);
    te->setFont(font);
    this->setCentralWidget(te);

    file=new QMenu("file");
    this->menuBar()->addMenu(file);
    edit=new QMenu("edit");
    this->menuBar()->addMenu(edit);
    build=new QMenu("build");
    this->menuBar()->addMenu(build);


    open=new QAction("open");
    open->setShortcut(tr("ctrl+o"));
    file->addAction(open);

    save=new QAction("save");
    save->setShortcut(tr("ctrl+s"));
    file->addAction(save);

    exit_=new QAction("eixt");
    exit_->setShortcut(tr("ctrl+q"));
    file->addAction(exit_);

    connect(open,SIGNAL(triggered()),this,SLOT(on_open_click()));
    connect(save,SIGNAL(triggered()),this,SLOT(on_save_click()));
    connect(exit_,SIGNAL(triggered()),this,SLOT(on_exit_click()));


    copy=new QAction("copy");
    copy->setShortcut(tr("ctrl+c"));
    edit->addAction(copy);
    paste=new QAction("paste");
    paste->setShortcut(tr("ctrl+v"));
    edit->addAction(paste);
    cut=new QAction("cut");
    cut->setShortcut(tr("ctrl+x"));
    edit->addAction(cut);

    connect(copy,SIGNAL(triggered()),this,SLOT(on_copy_click()));
    connect(paste,SIGNAL(triggered()),this,SLOT(on_paste_click()));
    connect(cut,SIGNAL(triggered()),this,SLOT(on_cut_click()));

    buildc=new QAction("buildc");
    buildc->setShortcut(tr("ctrl+m"));
    build->addAction(buildc);
    run=new QAction("run");
    run->setShortcut(tr("ctrl+r"));
    build->addAction(run);

    connect(buildc,SIGNAL(triggered()),this,SLOT(on_buildc_click()));
    connect(run,SIGNAL(triggered()),this,SLOT(on_run_click()));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete te;
    delete file;
    delete edit;
    delete build;

    delete open;
    delete save;
    delete exit_;
    delete copy;
    delete paste;
    delete cut;
    delete buildc;
    delete run;

}
void MainWindow::on_open_click()
{
    file_path=QFileDialog::getOpenFileName();
    if(file_path.isEmpty())
        return;

    FILE *p =fopen(file_path.toStdString().data(),"r");
    if(p==NULL)
    {
        QMessageBox::information(this,"notify","open file failure.");
        return;
    }

    QString content;
    char buf[1024]={0};

    while(!feof(p)){
        content+=  fgets(buf,sizeof(buf),p);
    }
    fclose(p);
    te->setText(content);

}

void MainWindow::on_save_click()
{

    file_path=QFileDialog::getSaveFileName();
    if(file_path==NULL)return;

    FILE *p=fopen(file_path.toStdString().data(),"w");
    fputs(te->toPlainText().toStdString().data(),p);
    fclose(p);

}


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

void MainWindow::on_copy_click()
{
    te->copy();
}
void MainWindow::on_paste_click()
{
    te->paste();

}
void MainWindow::on_cut_click()
{
    te->cut();
}

void MainWindow::on_buildc_click()
{

    QString dest=file_path;
    dest.replace(".c","");
    system(("gcc -o "+ dest +" "+ file_path).toStdString().data());
    QMessageBox::information(this,"notify","build success!");

}

void MainWindow::on_run_click()
{

    QString dest=file_path;
    dest.replace(".c","");
    system(dest.toStdString().data());


}