当前位置:网站首页>QT database application 22 file coding format recognition
QT database application 22 file coding format recognition
2022-06-10 12:58:00 【feiyangqingyun】
One 、 Preface
In the process of data import and export , If there are more application scenarios , I believe you will encounter a problem is the problem of file coding , Some files are ANSI code , Some are utf8 code , Some are utf8 belt bom code , If different file codes use the same encoding format to parse the read data , You will definitely encounter the problem of garbled code , This is not Qt The problem of , It's not something Qt The problem of disorderly code , Instead, you need to identify the file code and then use the corresponding code to read the content , So there will be no garbled code , Of course, the appearance of garbled code must be Chinese , If all the documents are in English numbers , No matter what code , It's not a mess .
So here comes the question , How to use the program to automatically identify the file coding format ? I searched all over the place and couldn't find the complete answer . Looking up the data, we learned that utf8 belt bom The encoding will have a fixed header byte EFBBBF, So it's easy to distinguish , because ANSI Coding and utf8 The encoding has no corresponding header byte identifier , So we need to take a turn to deal with , Still read the three bytes of the header , use QTextCodec Of toUnicode Function conversion , Of the conversion result ConverterState The number of available characters can be recognized , If the quantity is greater than 0 That is the ANSI code , Need to use gbk Decode .
Two 、 Functional characteristics
- The component also integrates exporting data to csv、xls、pdf And print data .
- All operations provide static methods without new, Various parameter settings such as data and attributes adopt structure data , It's very convenient .
- Support at the same time QTableView、QTableWidget、QStandardItemModel、QSqlTableModel Equal data source .
- Provide static methods to pass in directly QTableView、QTableWidget Control , Automatically identify column names 、 Column width and data content .
- Each set of functions provides a separate complete example , Note details , Very suitable for all stages Qter The programmer .
- Original export data mechanism , Do not rely on any office Third party libraries such as components or operating systems , Support embedded linux.
- Speed super fast ,9 A field 10 Ten thousand rows of data only need 2 Seconds to complete .
- It only takes four steps to quickly export massive data, such as 100W Record to Excel.
- At the same time, it provides direct write data interface and multithread write data interface , The main interface is not stuck .
- You can set the title 、 Subtitle 、 Table name .
- You can set the field name of the exported data 、 Name 、 Column width .
- You can set the automatic stretch filling of the end column , The default stretch is more beautiful .
- You can set whether to enable the verification and filtering data , When enabled, the data conforming to the rules will be displayed in special colors .
- You can specify the columns to check 、 Validation rules 、 Check value 、 Check value data type .
- Validation rules support Exactly equal to ==、 Greater than >、 Greater than or equal to >=、 Less than <、 Less than or equal to <=、 It's not equal to !=、 contain contains.
- Check value data type supports integer int、 floating-point float、 Double precision type double, Default text string type .
- You can set random background color and column set that needs random background color .
- Support group output data , For example, output data by device group , Convenient view .
- Can be set up csv Separator 、 Line content separator 、 Sub content separator .
- You can set the border width 、 Automatically fill in the data type , The default automatic data type is on .
- You can set whether to turn on the data cell style , Not on by default , Not opening can save about 30% The file volume of .
- Horizontal layout can be set 、 Paper margins, etc , Like exporting to pdf And print data .
- Provide mixed arrangement of pictures and texts to export data to pdf And print examples , Automatic paging , Support multi graph .
- Provide a plot template that includes both horizontal and vertical layout examples .
- Provide static functions to export control screenshots to pdf file .
- Provides a static function to convert an image into pdf file .
- Providing static functions will csv Document conversion xls file , Support parameter settings such as column width and table name .
- You can set the field alignment style for each column 、 Content alignment style , Including left alignment 、 Align center 、 Right alignment .
- Super flexibility , Free to change the source set alignment 、 Text color 、 Background color, etc .
- Support any excel Form software , Including but not limited to excel2003-2021、wps、openoffice etc. .
- pure Qt To write , Support any Qt edition + Any compiler + Any system .
3、 ... and 、 Experience address
- Experience address :https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A Extraction code :o05q file name :bin_dataout.zip
- Domestic site :https://gitee.com/feiyangqingyun
- International sites :https://github.com/feiyangqingyun
- Personal home page :https://blog.csdn.net/feiyangqingyun
- Zhihu Homepage :https://www.zhihu.com/people/feiyangqingyun/
Four 、 design sketch

5、 ... and 、 Related codes
// Check the file code 0=ANSI 1=UTF-16LE 2=UTF-16BE 3=UTF-8 4=UTF-8BOM
int DataCsv::findCode(const QString &fileName, QString &flag)
{
// Assume default encoding utf8
int code = 3;
flag = "UTF-8";
QFile file(fileName);
if (file.open(QIODevice::ReadOnly)) {
// Read 3 Bytes for judgment
QByteArray buffer = file.read(3);
quint8 b1 = buffer.at(0);
quint8 b2 = buffer.at(1);
quint8 b3 = buffer.at(2);
if (b1 == 0xFF && b2 == 0xFE) {
code = 1;
flag = "UTF-16LE";
} else if (b1 == 0xFE && b2 == 0xFF) {
code = 2;
flag = "UTF-16BE";
} else if (b1 == 0xEF && b2 == 0xBB && b3 == 0xBF) {
code = 4;
flag = "UTF-8BOM";
} else {
// Try to use utf8 transformation , If the number of available characters is greater than 0, It means ansi code
QTextCodec::ConverterState state;
QTextCodec *codec = QTextCodec::codecForName("utf-8");
codec->toUnicode(buffer.constData(), buffer.size(), &state);
if (state.invalidChars > 0) {
code = 0;
flag = "ANSI";
}
}
file.close();
}
return code;
}
边栏推荐
- Shadergraph - Crystal
- 【FLinlk】Flink小坑之kerberos动态认证
- Qt数据库应用22-文件编码格式识别
- Introduction of Altium Designer
- Count the number and average value of natural numbers whose sum of bits within 100 is 7
- 性能测试方案(计划)模板
- C # balanced weight distribution
- 日本版arXiv凉得一批:2个多月了,才收到37篇论文
- Learning of cc2642r Bluetooth MCU chip
- Unity3D开发MR实现模型遮挡与透明地面接收阴影
猜你喜欢

蚂蚁金服杨军:蚂蚁数据分析平台的演进及数据分析方法的应用

(5) Class, object and class file splitting operation (2)

UML class diagram

从解读 BDC 自动生成的代码谈起,讲解 SAPGUI 的程序组成部分

JTAG-to-AXI Master调试AXI BRAM Controller

Introduction of Altium Designer

Start with interpreting the code automatically generated by BDC, and explain the trial version of the program components of sapgui

技术分享| 快对讲,全球对讲

Shadergraph - 301 bouncing ball

VDMA commissioning summary
随机推荐
今天,一对情侣拿下香港最大电商IPO
Can chip learning of max3051
(6) Classes and objects, object initialization and copy constructors (3)
Ant financial services Yang Jun: evolution of ant data analysis platform and application of data analysis methods
In June, 2022, China Database ranking: tidb made a comeback to win the crown, and Dameng was dormant and won the flowers in May
TIDB 初级课程体验 8 (集群的管理维护, 添加一个TIKV节点)
Stereo vision based semantic 3D object and ego motion tracking for automotive driving
Mr developed by unity3d realizes model occlusion and transparent ground receiving shadow
If the files and graphics are lost, it means that you don't need the office developed by yourself
Today, a couple won the largest e-commerce IPO in Hong Kong
Altium Allegro PADS到底该选哪个EDA设计软件
Practical cases, in-depth analysis
VDO-SLAM源码阅读笔记[2] local optimization和global optimization
Stm32f407 clock tree and system clock learning notes
使用unique快速删除重复元素
Don't mistake "it informatization" for "super project"
CMakeLists. Txt how to write
Element close and destroy the pop-up box: clear the data cache in the pop-up box (organize)
Office technical lecture: punctuation - English - Encyclopedia
启牛能开户吗,启牛在APP上可以直接开通券商安全吗