当前位置:网站首页>Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
Qt读取文件中内容(通过判断GBK UTF-8格式进行读取显示)
2022-08-02 08:13:00 【LCH南安】
目录
1.读取GBK文件
2.读取UTF8格式
代码实现
Widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QFile>
#include <QTextCodec>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->pushButton,&QPushButton::clicked,[=](){
//打开文件
QString path= QFileDialog::getOpenFileName(this,"打开文件","D:\\AllProject_Code\\QtWorkSpace\\Online_Space\\QtLearn-master\\day3\\Doc");
//将路径放入lineEdit
ui->lineEdit->setText(path);
//读取文件内容 默认打开UTF-8格式
QFile file(path);
//设置打开方式
file.open(QIODevice::ReadOnly);
//获取文件内容
//QByteArray array = file.readAll();
QByteArray array;
while(!file.atEnd())
{
//按行读取
array +=file.readLine();
}
QString text=GetCorrectUnicode(array);
//将内容放入textEdit
ui->textEdit->setText(text);//两种格式均可读取
//对文件对象进行关闭
file.close();
});
}
QString Widget::GetCorrectUnicode(const QByteArray &text)
{
QTextCodec::ConverterState state;
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString strtext = codec->toUnicode( text.constData(), text.size(), &state);
if (state.invalidChars > 0)
{
strtext = QTextCodec::codecForName( "GBK" )->toUnicode(text);
}
else
{
strtext = text;
}
return strtext;
}
Widget::~Widget()
{
delete ui;
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
QString GetCorrectUnicode(const QByteArray &text);
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
参考链接:(16条消息) Qt自动判断文本文件的编码格式(QT自定义文件编辑器)_立冲君也的博客-CSDN博客_qt获取文件编码格式
https://blog.csdn.net/ZHLCHLC/article/details/124147279
边栏推荐
- Ansible 学习总结(11)—— task 并行执行之 forks 与 serial 参数详解
- 小说里的编程 【连载之二十三】元宇宙里月亮弯弯
- day_05 time 模块
- Axial Turbine Privacy Policy
- PyCharm usage tutorial (detailed version - graphic and text combination)
- 文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
- MFC最详细入门教程[转载]
- The crawler video crawl tools you get
- 小康股份更名赛力斯,如何走出一条高端产品的“丝绸之路”?
- Biotin hydrazide HCl|CAS:66640-86-6|生物素-酰肼盐酸盐
猜你喜欢
A young man with strong blood and energy actually became a housekeeper. How did he successfully turn around and change careers?
Redisson报异常attempt to unlock lock, not locked by current thread by node id解决方案
node(三) 模块化
Biotinyl Cystamine|CAS:128915-82-2|生物素半胱胺
MFC最详细入门教程[转载]
pnpm: Introduction
Application and case analysis of CASA model and CENTURY model
Biotin - LC - Hydrazide | CAS: 109276-34-8 | Biotin - LC - Hydrazide
用C写小游戏(三子棋)
如何做好项目管理
随机推荐
Application and case analysis of CASA model and CENTURY model
Codeforces Round #811 (Div. 3)无DF
A little bit of knowledge - why do not usually cook with copper pots
按键控制流水灯(计时器)
redis的安装与应用
[OC学习笔记]ARC与引用计数
PostgreSQL learning summary (11) - PostgreSQL commonly used high-availability cluster solutions
Shell变成规范与变量
mysqldump --set-gtid-purged=OFF
prometheus monitoring mysql_galera cluster
为什么都推荐使用wordpress, 而不是 phpcms 这些国内的CMS呢?
52.【bool类型输入任何非0数值不为1的版本原因】
Mysql Mac版下载安装教程
【开源项目】X-TRACK源码分析
etcd implements large-scale service governance application combat
普林斯顿微积分读本03第二章--编程实现函数图像绘制、三角学回顾
JSP中page指令的import命令具有什么功能呢?
[ansible] playbook explains the execution steps in combination with the project
抓包工具Charles修改Response步骤
postman下载安装汉化及使用