当前位置:网站首页>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
边栏推荐
- 下一个排列
- PyCharm usage tutorial (detailed version - graphic and text combination)
- 软件测试技术之解析图灵测试离我们还有多远
- 【开源项目】X-TRACK源码分析
- (Note) AXIS ACASIS DT-3608 Dual-bay Hard Disk Array Box RAID Setting
- [ansible]playbook结合项目解释执行步骤
- 轴流式水轮机隐私政策
- mysqldump --set-gtid-purged=OFF
- 三维体尺测量
- 文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
猜你喜欢

第3周学习:ResNet+ResNeXt

EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network

mysql 中 in 的用法

C语言基础_结构体

PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image

ip地址那点事(二)

类和对象【下】

(Note)阿克西斯ACASIS DT-3608双盘位硬盘阵列盒RAID设置

cas: 139504-50-0 Maytansine DM1|Mertansine|
![Three types of [OC learning notes] Block](/img/40/edf59e6e68891ea7c9ab0481fe7bfc.png)
Three types of [OC learning notes] Block
随机推荐
Technology Cloud Report: To realize the metaverse, NVIDIA starts from building an infrastructure platform
52. [Bool type input any non-0 value is not 1 version reason]
Analysis of software testing technology How far is Turing test from us
那些年我们踩过的 Flink 坑系列
[OC学习笔记]weak的实现原理
如何做好项目管理
postman下载安装汉化及使用
52.【bool类型输入任何非0数值不为1的版本原因】
R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re
In a recent build figure SLAM, and locate the progress
近期在SLAM建图和定位方面的进展
小康股份更名赛力斯,如何走出一条高端产品的“丝绸之路”?
EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
Button to control the running water light (timer)
C语言基础_共用体
etcd implements large-scale service governance application combat
传递泛型给JSX元素
CASA模型、CENTURY模型应用与案例分析
小说里的编程 【连载之二十一】元宇宙里月亮弯弯
[OC学习笔记]Block三种类型
