函数代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <stdlib.h>
#define cout qDebug()
#include <QFileDialog>
#include <QTextCodec>
//定义一个枚举类型
enum MyCode
{
utf_8, gbk
};
QString path = "";
enum MyCode flag;
QTextCodec *co;
void MainWindow::test() //类中声明,类外定义,并且定义是需要加上作用域。
{
flag = utf_8;
//QLable的操作'
//给标签设置内容
ui->label->setText("hello qt");
//获取标签的内容,返回值为QString
QString str = ui->label->text();
cout << "str = " << str;
//QTextEdit操作
ui->textEdit->setText("hello Mike");
str = ui->textEdit->toPlainText();
cout << "str = " << str;
}
//构造函数,主要做初始化操作
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//QLable的操作'
//给标签设置内容
//ui->label->setText("hello qt");
//test();
//给标签设置内容:
//ui->label->setText("你好!欢迎来到程序的世界");
//要想实现中文,必须为utf-8编码
co = QTextCodec::codecForName("GBK");
//co->fromUnicode(); //把UTF-8转化为GBK
//co->toUnicode(); //把一个GBK编码的字符串转化成 UTF-8
//const char *str = "你好!欢迎来到程序的世界";
//ui->label->setText(co->toUnicode(str));
ui->textEdit->setText(co->toUnicode("嗨! Mike,让我们开始吧!"));
}
//析构函数,主要做清理操作(对象关闭前)
MainWindow::~MainWindow()
{
delete ui;
}
//打开按钮的处理函数
void MainWindow::on_actionopen_triggered()
{
//1. 打开一个文件,获取文件的路径
path = QFileDialog::getOpenFileName();
//cout << "path = "<<path;
if(path == "") //没有选择路径
if(path.isEmpty())
{
return;
}
//cout << "11111111111111";
//2. 通过Qt控件获取路径,此路径是utf-8编码,需要转换成gbk编码
//同时将Qstring类型转换为char *
char *fileName = co->fromUnicode(path).data();
//cout << "filename = " << fileName;
//3. 打开文件,fopen(),fopen()如果有中文,需要gbk
FILE *fp = fopen(fileName, "rb");
if(NULL == fp)
{
cout << "on_actionopen_triggered fopen error";
return;
}
//4. 循环读文件类型fgets(), 读取的类型是char *
char buf[1024];
QString str = "";
while(!feof(fp))
{
memset(buf, 0, sizeof(buf));
fgets(buf, sizeof(buf), fp);
if(flag == utf_8)
str = str + buf;
else if(flag == gbk)
{
str = str + co->toUnicode(buf);
}
}
//5. 把读取的内容给编辑区setText()
ui->textEdit->setText(str);
//6.关闭文件
fclose(fp);
}
void MainWindow::on_actionUTF_8_triggered()
{
//按下此按钮选择utf-8编码
flag = utf_8;
ui->label->setText(co->toUnicode("当前以utf-8编码显式"));
}
void MainWindow::on_actionGBK_triggered()
{
flag = gbk;
ui->label->setText("gbk");
ui->label->setText(co->toUnicode("当前以gbk编码显式"));
}
//另存为 按钮的函数槽
void MainWindow::on_actionsaveas_triggered()
{
//1. 获取路径
path = QFileDialog::getSaveFileName();
if("" == path)
{
return;
}
//2. 将路径转换为gbk类型
char *fileName = co->fromUnicode(path).data();
//3. 打开文件,fopen(),有中文需要gbk
FILE *fp = fopen(fileName, "wb");
if(NULL == fp)
{
cout << "on_actionsaveas_triggered fopen err";
return;
}
//4. 获取编辑区的内容(Qstring类型)
QString str = ui->textEdit->toPlainText();
//5. 将编辑区的内容QString转换为char *
const char *buf = str.toStdString().data();
//6. 将编辑区的文件写入文件 fputs
fputs(buf, fp);
//7. 关闭文件
fclose(fp);
}
//撤销按钮
void MainWindow::on_actionundo_triggered()
{
ui->textEdit->undo();
}
//复制按钮
void MainWindow::on_actioncpy_triggered()
{
ui->textEdit->copy();
}
//粘贴按钮
void MainWindow::on_actionpaste_triggered()
{
ui->textEdit->paste();
}
//剪切按钮
void MainWindow::on_actioncut_triggered()
{
ui->textEdit->cut();
}
//编译按钮
void MainWindow::on_actioncompile_triggered()
{
if(path.isEmpty())
return;
//cout << "path = " << path;
QString demo = path;
demo.replace(".c", ""); //将.c替换为空字符
//cout << "demo = " << demo;
QString cmd = QString("gcc %1 -o %2").arg(path).arg(demo);
cout << "cmd = "<<cmd;
//如果有中文,需要转化gbk,同时将QString 转换为 char *;
int ret = system(co->fromUnicode(cmd).data());
//cout << "ret = " << ret;
if(ret != 0)
{
cmd = QString("cmd /k gcc %1 -o %2").arg(path).arg(demo);
system(co->fromUnicode(cmd).data());
return;
}
cmd = QString("cmd /k %1").arg(demo);
system(co->fromUnicode(cmd).data());
}
void MainWindow::on_actionexit_triggered()
{
exit(0);
}
void MainWindow::on_actionsave_triggered()
{
if(path.isEmpty())
{
//1.获取路径
path = QFileDialog::getSaveFileName();
}
//2. 将路径转换为gbk类型
char *fileName = co->fromUnicode(path).data();
//3. 打开文件,fopen(),有中文需要gbk
FILE *fp = fopen(fileName, "wb");
if(NULL == fp)
{
cout << "on_actionsave_triggered fopen err";
return;
}
//4. 获取编辑区的内容(Qstring类型)
QString str = ui->textEdit->toPlainText();
//5. 将编辑区的内容QString转换为char *
const char *buf = str.toStdString().data();
//6. 将编辑区的文件写入文件 fputs
fputs(buf, fp);
//7. 关闭文件
fclose(fp);
}
void MainWindow::on_actionnew_triggered()
{
//编辑区清空
//ui->textEdit->setText("");
ui->textEdit->clear();
//路径情况
//path = "";
path.clear();
}
网友评论