c语言入门:用qt实现简单IDE
正在学习C语言的大家,知道用qt如何实现实现简单IDE吗?想是小编为大家搜集整理出来的有关于c语言入门:用qt实现简单IDE,希望可以帮助到大家!
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 | #include "mainwindow.h" #include <qapplication> int main( int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.resize( 1000 , 800 ); w.show(); return a.exec(); }</qapplication> |
mainwindow.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <qmainwindow> #include <qtextedit> #include <qmenu> #include <qmenubar> 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 </qmenubar></qmenu></qtextedit></qmainwindow> |
mainwindow.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #include "mainwindow.h" #include "ui_mainwindow.h" #include <stdlib.h> #include <qfiledialog> #include <qmessagebox> //#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() { ui; te; file; edit; build; open; save; exit_; copy; paste; cut; buildc; 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()); } </qmessagebox></qfiledialog></stdlib.h> |
【c语言入门:用qt实现简单IDE】相关文章: