您好,登錄后才能下訂單哦!
項目創(chuàng)建
基類選擇
項目文件
編譯運行
項目文件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編輯環(huán)境,安裝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; //在堆中創(chuàng)建一個QTextEdit實例 text1->setFontPointSize(14); //設置窗體的字體大小 this->setCentralWidget(text1); //將TextEdit控件加入到主窗口的工作區(qū) //文本內容在保存前是否發(fā)生變動 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 菜單的時候,執(zhí)行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()));//當文本內容發(fā)生變化時,觸發(fā)on_changed函數 connect(comp_comp,SIGNAL(triggered()),this,SLOT(on_comp()));//當文本內容發(fā)生變化時,觸發(fā)on_changed函數 connect(run_comp,SIGNAL(triggered()),this,SLOT(on_run()));//當文本內容發(fā)生變化時,觸發(fā)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)//在點擊編譯按鈕,如果文本內容發(fā)生變化,就自動保存 { 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()); }
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。