当前位置:网站首页>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
边栏推荐
- etcd implements large-scale service governance application combat
- BGP solves routing black hole through MPLS
- What is NoSQL?Databases for the cloud-scale future
- pnpm:简介
- EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
- MySQL Workbench 安装及使用
- mysql 中 in 的用法
- postman使用方法
- Spark 系统性学习笔记系列
- Flink 系统性学习笔记系列
猜你喜欢

MySQL读写分离与主从延迟

BGP通过MPLS解决路由黑洞

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

王学岗-编译出运行的文件

MySQL 中 count() 和 count(1) 有什么区别?哪个性能最好?
![Three types of [OC learning notes] Block](/img/40/edf59e6e68891ea7c9ab0481fe7bfc.png)
Three types of [OC learning notes] Block

【C】关于柔性数组.简要的谈谈柔性数组

USACO美国信息学奥赛竞赛12月份开赛,中国学生备赛指南

Biotin hydrazide HCl|CAS:66640-86-6|生物素-酰肼盐酸盐

PyQt5 (a) PyQt5 installation and configuration, read from the folder and display images, simulation to generate the sketch image
随机推荐
R language plotly visualization: use the plotly visualization model to predict the true positive rate (True positive) TPR and false positive rate (False positive) FPR curve under different thresholds
2022-7-31 12点 程序爱生活 恒指底背离中,有1-2周反弹希望
Redisson的看门狗机制
What attributes and methods are available for page directives in JSP pages?
Wang Xuegang - compiled shipment line file
Postman download localization of installation and use
How Engineers Treat Open Source --- A veteran engineer's heartfelt words
为什么都推荐使用wordpress, 而不是 phpcms 这些国内的CMS呢?
redis-desktop-manager下载安装
Flink 程序剖析
【开源项目】X-TRACK源码分析
pnpm的安装与使用
MySQL Workbench 安装及使用
IO进程线程->进程->day4
IO process thread -> process -> day4
pnpm: Introduction
MySQL读写分离与主从延迟
The custom table form
PyCharm使用教程(较详细,图+文)
MySQL ODBC驱动简介
