天天看點

C基礎學習筆記——01-C基礎第14天(IDE)

在學習C基礎總結了筆記,并分享出來。有問題請及時聯系部落客:​​Alliswell_WP​​,轉載請注明出處。

01-C基礎第14天(IDE)

目錄:

QT建立IDE

項目:記事本

QT建立IDE

注意:1.QT對中文相容性不好(建路徑也不要有中文),2.保證gcc可用

項目:記事本

設定項目注意:

1)選中“圖形化界面”

2)點選“在這裡插入”,輸入“檔案”,然後回車,點選“在這裡插入”:輸入拼音(後續改成中文)dakai,xinjian,baocun,lingcunwei,點選“添加分隔符”再次點選“在這裡插入”:輸入拼音tuichu。

改中文:法一:找到dakai,輕按兩下打開“編輯動作”,文本後dakai改成“打開”

  法二:選中dakai,在右側“屬性”欄找到“text”,把後邊的dakai改成“打開”

優化:在會彈出新的對話框的後邊加三個點,如:“打開...”或者快捷鍵

3)點選“在這裡插入”,輸入“編輯”,然後回車,點選“在這裡插入”:輸入拼音chexiao,fuzhi,zhantie,jianqie。

注意:按住某個,如“fuzhi”可以更改位置。後續按2)改成中文。

4)點選“在這裡插入”,輸入“建構”,然後回車,點選“在這裡插入”:輸入拼音bianji。後續按2)改成中文。

5)點選“在這裡插入”,輸入“幫助”,然後回車,點選“在這裡插入”:輸入拼音guanyu。後續按2)改成中文。

6)添加文本框,搜尋“Text Edit”,選中,拖拽到圖形化界面,更改文本框大小。

7)實作可以擴大縮小:先點選前景界面“文本框”,在點選後景界面“背景”,選中上方“九宮格”。邊距設定:在對象“MainWindow”的屬性欄(下邊)中找到“Layout”,把“layoutLeftMargin”設定為0,“layoutTopMargin”設定為0,“layoutRightMargin”設定為0,“layoutBottomMargin”設定為0。把下邊的邊去掉:在對象“MainWindow”的“menuBar”的“statusBar”,選中,右鍵點選“移除”。

8)更改标題欄:先點選“MainWindow”,然後在對象“MainWindow”的屬性欄(下邊)中找到“windowTitle”,“MainWindow”更改為“內建化開發環境”。

注意:千萬别改“objectName”!

9)更改圖示:先把要用的圖示app.ico放到項目檔案夾下。QT界面“左側”點選編輯,選中:IDE.pro,打開,然後在租後增加一句:RC_ICONS = app.ico(app.ico為圖示的名字)。

注意:程序卡死,或打開多個運作出錯,無法關閉,點選紅色的框!

10)在下邊名稱處右擊“轉到槽”,選中“triggered()”,點選“ok”,出現“事件”,可以編輯事件on_action_4_triggered():

1 void MainWindow::on_action_4_triggered()
 2 {
 3     //打開檔案
 4     /*1、foprn(檔案名,打開方式)
 5      * 2、判斷可用性
 6      * 3、讀取内容
 7      * 4、關閉
 8      */
 9     //通過對話框打開檔案 擷取檔案路徑 QString
10     fileName = QFileDialog::getOpenFileName();
11     //cout<<"===="<<fileName<<"====";
12     //将QString轉化為char *
13     QTextCodec * codec = QTextCodec::codecForName("GBK");
14 
15     //char * file =codec->fromUnicode(fileName).data();看是否可用,如果不可用,用下邊的兩句
16     QByteArray ba = fileName.toLatin1();
17     char * file  =ba.data();
18     FILE * fp = fopen(file,"r");
19     if(!fp)
20     return;
21     char  buf[1024];
22     QString txt;
23     while(!feof(fp))
24     {
25         memset(buf,0,1024);
26         fgets(buf,1024,fp);
27         //cout<<buf;
28         txt +=codec->toUnicode(buf);
29     }
30 
31     //為文本輸入标簽設定内容
32     ui->textEdit->setText(txt);
33 
34     fclose(fp);
35 }      

注意:需要打開的文本編碼是GBK格式的!

如需要改顯示字型,在對象“MainWindow”的屬性欄(下邊)中找到“font”在“font”中更改

擴充:構造函數和析構函數

C基礎學習筆記——01-C基礎第14天(IDE)

11)on_actionBaocun_triggered“儲存”代碼:

1 void MainWindow::on_actionBaocun_triggered()
 2 {
 3     if(fileName==NULL)
 4     //打開檔案
 5     fileName = QFileDialog::getSaveFileName();
 6 
 7    //轉碼
 8     QTextCodec * codec  = QTextCodec::codecForName("GBK");
 9 
10     char * file = codec->fromUnicode(fileName).data();
11 
12     //讀取textedit
13     QString txt = ui->textEdit->toPlainText();
14     //轉成char*
15     const char * buf = txt.toStdString().data();
16 
17     saveFile(file,buf);
18 
19 
20 }      

12)建立

1 void MainWindow::on_action_5_triggered()
2 {
3     //ui->textEdit->setText("");
4 
5     //提示是使用者儲存
6     ui->textEdit->clear();
7     fileName.clear();
8 }      

13)另存為

1 void MainWindow::on_action_7_triggered()
2 {
3 
4     //提示是使用者儲存
5     exit(0);
6 }      

14)撤銷

1 void MainWindow::on_action_8_triggered()
2 {
3     //撤銷
4     ui->textEdit->undo();
5 }      

15)拷貝

1 void MainWindow::on_action_9_triggered()
2 {
3     //拷貝
4     ui->textEdit->copy();
5 }      

16)粘貼

1 void MainWindow::on_action_10_triggered()
2 {
3     //粘貼
4     ui->textEdit->paste();
5 }      

17)剪切

1 void MainWindow::on_action_11_triggered()
2 {
3     //剪切
4     ui->textEdit->cut();
5 }      

 18)編譯

1 void MainWindow::on_action_12_triggered()
 2 {
 3     /* 1、儲存檔案(如果檔案儲存)
 4      * 2、組裝gcc格式
 5      * 3、編譯 如果出錯提示錯誤資訊   運作程式
 6      *
 7      */
 8     if(fileName ==NULL)
 9     {
10 
11         fileName = QFileDialog::getSaveFileName();
12         QTextCodec * codec = QTextCodec::codecForName("GBK");
13         const char * file = codec->fromUnicode(fileName).data();
14 
15         QString txt = ui->textEdit->toPlainText();
16         const char * buf = txt.toStdString().data();
17         saveFile(file,buf);
18     }
19     else
20     {
21         //gcc -o D:\a.exe D:\a.c
22 
23         QString des = fileName;
24         des.replace(".c",".exe");
25         //cout<<des;
26         char  comm[1024]="gcc -o ";
27         strcat(comm,des.toStdString().data());
28         strcat(comm," ");
29         strcat(comm,fileName.toStdString().data());
30 
31         //程式編譯成功system 傳回值為0
32         if(!system(comm))
33         {
34             char cmd[256]="cmd /k ";
35             strcat(cmd,des.toStdString().data());
36             cout<<cmd;
37             system(cmd);
38         }
39         else
40         {
41             //cmd /k gcc -o D:\a.exe D:\a.c
42             char cmd[256] = "cmd /k  ";
43             strcat(cmd,comm);
44             system(cmd);
45         }
46     }
47 
48 
49 }      

mainwindow.cpp代碼如下:

1 #include "mainwindow.h"
  2 #include "ui_mainwindow.h"
  3 
  4 #include <QTextCodec>
  5 #include <QFileDialog>
  6 #include <QString>
  7 #include <QDebug>
  8 #include <QByteArray>
  9 #include <string.h>
 10 #define cout qDebug()
 11 
 12 
 13 QString fileName=NULL;
 14 MainWindow::MainWindow(QWidget *parent) :
 15     QMainWindow(parent),
 16     ui(new Ui::MainWindow)
 17 {
 18     ui->setupUi(this);
 19 }
 20 
 21 MainWindow::~MainWindow()
 22 {
 23     delete ui;
 24 }
 25 
 26 void MainWindow::saveFile(const char * file,const char * buf)
 27 {
 28     FILE * fp = fopen(file,"w");
 29     if(!fp)
 30         return;
 31 
 32     //儲存檔案
 33     fputs(buf,fp);
 34 
 35     fclose(fp);
 36 }
 37 
 38 void MainWindow::on_action_4_triggered()
 39 {
 40     //打開檔案
 41     /*1、foprn(檔案名,打開方式)
 42      * 2、判斷可用性
 43      * 3、讀取内容
 44      * 4、關閉
 45      */
 46     //通過對話框打開檔案 擷取檔案路徑 QString
 47     fileName = QFileDialog::getOpenFileName();
 48     //cout<<"===="<<fileName<<"====";
 49     //将QString轉化為char *
 50     QTextCodec * codec = QTextCodec::codecForName("GBK");
 51 
 52     //char * file =codec->fromUnicode(fileName).data();
 53     QByteArray ba = fileName.toLatin1();
 54     char * file  =ba.data();
 55     FILE * fp = fopen(file,"r");
 56     if(!fp)
 57     return;
 58     char  buf[1024];
 59     QString txt;
 60     while(!feof(fp))
 61     {
 62         memset(buf,0,1024);
 63         fgets(buf,1024,fp);
 64         //cout<<buf;
 65         txt +=codec->toUnicode(buf);
 66     }
 67 
 68     //為文本輸入标簽設定内容
 69     ui->textEdit->setText(txt);
 70 
 71     fclose(fp);
 72 }
 73 
 74 void MainWindow::on_actionBaocun_triggered()
 75 {
 76     if(fileName==NULL)
 77     //打開檔案
 78     fileName = QFileDialog::getSaveFileName();
 79 
 80    //轉碼
 81     QTextCodec * codec  = QTextCodec::codecForName("GBK");
 82 
 83     char * file = codec->fromUnicode(fileName).data();
 84 
 85     //讀取textedit
 86     QString txt = ui->textEdit->toPlainText();
 87     //轉成char*
 88     const char * buf = txt.toStdString().data();
 89 
 90     saveFile(file,buf);
 91 
 92 
 93 }
 94 
 95 void MainWindow::on_action_5_triggered()
 96 {
 97     //ui->textEdit->setText("");
 98 
 99     //提示是使用者儲存
100     ui->textEdit->clear();
101     fileName.clear();
102 }
103 
104 void MainWindow::on_action_7_triggered()
105 {
106 
107     //提示是使用者儲存
108     exit(0);
109 }
110 
111 void MainWindow::on_action_8_triggered()
112 {
113     //撤銷
114     ui->textEdit->undo();
115 }
116 
117 void MainWindow::on_action_9_triggered()
118 {
119     //拷貝
120     ui->textEdit->copy();
121 }
122 
123 void MainWindow::on_action_10_triggered()
124 {
125     //粘貼
126     ui->textEdit->paste();
127 }
128 
129 void MainWindow::on_action_11_triggered()
130 {
131     //剪切
132     ui->textEdit->cut();
133 }
134 
135 void MainWindow::on_action_12_triggered()
136 {
137     /* 1、儲存檔案(如果檔案儲存)
138      * 2、組裝gcc格式
139      * 3、編譯 如果出錯提示錯誤資訊   運作程式
140      *
141      */
142     if(fileName ==NULL)
143     {
144 
145         fileName = QFileDialog::getSaveFileName();
146         QTextCodec * codec = QTextCodec::codecForName("GBK");
147         const char * file = codec->fromUnicode(fileName).data();
148 
149         QString txt = ui->textEdit->toPlainText();
150         const char * buf = txt.toStdString().data();
151         saveFile(file,buf);
152     }
153     else
154     {
155         //gcc -o D:\a.exe D:\a.c
156 
157         QString des = fileName;
158         des.replace(".c",".exe");
159         //cout<<des;
160         char  comm[1024]="gcc -o ";
161         strcat(comm,des.toStdString().data());
162         strcat(comm," ");
163         strcat(comm,fileName.toStdString().data());
164 
165         //程式編譯成功system 傳回值為0
166         if(!system(comm))
167         {
168             char cmd[256]="cmd /k ";
169             strcat(cmd,des.toStdString().data());
170             cout<<cmd;
171             system(cmd);
172         }
173         else
174         {
175             //cmd /k gcc -o D:\a.exe D:\a.c
176             char cmd[256] = "cmd /k  ";
177             strcat(cmd,comm);
178             system(cmd);
179         }
180     }
181 
182 
183 }
184 
185 void MainWindow::on_action_6_triggered()
186 {
187     //打開檔案
188     fileName = QFileDialog::getSaveFileName();
189 
190    //轉碼
191     QTextCodec * codec  = QTextCodec::codecForName("GBK");
192 
193     char * file = codec->fromUnicode(fileName).data();
194 
195     //讀取textedit
196     QString txt = ui->textEdit->toPlainText();
197     //轉成char*
198     const char * buf = txt.toStdString().data();
199 
200     saveFile(file,buf);
201 }      

mainwindow.h代碼如下:

1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 namespace Ui {
 7 class MainWindow;
 8 }
 9 
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
13 
14 public:
15     explicit MainWindow(QWidget *parent = 0);
16     ~MainWindow();
17 
18 private slots:
19     void on_action_4_triggered();
20 
21     void on_actionBaocun_triggered();
22 
23     void on_action_5_triggered();
24 
25     void on_action_7_triggered();
26 
27     void on_action_8_triggered();
28 
29     void on_action_9_triggered();
30 
31     void on_action_10_triggered();
32 
33     void on_action_11_triggered();
34 
35 
36     void saveFile(const char * file,const char * buf);
37 
38     void on_action_12_triggered();
39 
40     void on_action_6_triggered();
41 
42 private:
43     Ui::MainWindow *ui;
44 };
45 
46 #endif // MAINWINDOW_H