在学习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”中更改
扩展:构造函数和析构函数

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