当前位置:网站首页>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;
}
边栏推荐
- Which EDA design software should Altium Allegro pads choose
- Six stone programming: talking about naming based on the position of word processing
- [mobile robot] principle of wheel odometer
- JS click the button to slide to the left
- eseses
- [flinlk] dynamic Kerberos authentication in Flink pit
- H5 pop up prompt layer - top, bottom, left and right center
- JTAG to Axi master debugging Axi Bram controller
- colmap源码阅读笔记[1] threading.cc
- Driver. JS - open source and independent interactive guidance tool library for web novices, powerful and highly customizable
猜你喜欢

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

Give root password for maintenace (or press Control-D to continue): solution
![[mobile robot] principle of wheel odometer](/img/91/fbe7ad8e37a70384f043a785d34865.png)
[mobile robot] principle of wheel odometer

编写程序,计算2/1+3/2+5/3+8/5.....的值。要求计算前n项之和,保留2位小数(该序列从第二项起,每一项的分子是前一项分子与分母的和,分母是前一项的分子)

The ability to register user names and passwords with the database

【Spark】(task8)SparkML中的pipeline通道建立

MySQL service evolution

Don't mistake "it informatization" for "super project"

Xshell 评估期已过怎么办? 按照以下步骤即可解决!

Recommended learning materials for Altium Designer
随机推荐
Unity3d 使用URP渲染管线实现AR阴影(阴影投射再透明地面)
JS use the Icheck plug-in to listen and get the value of the checkbox
使用unique快速删除重复元素
Can qiniu open an account? Is it safe to open an account in qiniu
Xshell 评估期已过怎么办? 按照以下步骤即可解决!
Driver. JS - open source and independent interactive guidance tool library for web novices, powerful and highly customizable
Ad-pcb schematic diagram learning (1)
蚂蚁金服杨军:蚂蚁数据分析平台的演进及数据分析方法的应用
Use soapUI tool to generate SMS interface code
CMakeLists.txt 如何编写
Shadergraph - 303 swaying grass
Shadergraph - Crystal
[mobile robot] principle of wheel odometer
Dynaslam ii: carefully coupled multi object tracking and slam
Mobile phone manufacturers "go back to their ancestors", only apple said no
Six stone programming: talking about naming based on the position of word processing
Can qiniu open an account? Can qiniu directly open the security of securities companies on the app
MySQL数据库(26):视图 view
excel异步导出
Colmap source code reading notes [1] threading cc