1 Qt中QString的比较
QString::compare("ab","ab"); //值为0;
QString::compare(“ab”,“df"); //值为<0;
QString::compare("df","ab"); //值为>0
2 Qt导出CSV中文乱码?
解决方案:加入编码设置即可。
改前(没有编码设置):
QFile file(fileName);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream in(&file);
in << m_csvStr;
file.close();
改后:
QFile file(fileName);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextCodec *codec = QTextCodec::codecForName("GBK"); //设置为ANSI编码
QTextStream in(&file);
in.setCodec(codec);
in << m_csvStr;
file.close();
3 读取CSV 或文件的时候中文乱码?
void Widget::readFromFile()
{
//指定为GBK
QTextCodec *codec = QTextCodec::codecForName("GBK");
//打开文件
QFile file("1.txt");
//如果打开文件失败,直接退出
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
return;
//当未到达文件结束位置
while(!file.atEnd())
{
//读取一行文本数据
QByteArray line = file.readLine();
//将读取到的行数据转换为Unicode
QString str = codec->toUnicode(line);
//将得到的字符设置到编辑框中显示, 主要是这里进行编码转换
ui->edit->append(str);
}
}
4 读取CSV
csvclass.h
#ifndef CSVCLASS_H
#define CSVCLASS_H
#include <QDebug>
#include <QFile>
class CsvClass
{
public:
CsvClass();
QList<QStringList> ReadFromCSV(QString PathName);
void SaveToCSV(QList<QStringList> CsvList,QString PathName);
};
#endif // CSVCLASS_H
csvclass.cpp
#include "csvclass.h"
#include <QTextCodec>
CsvClass::CsvClass()
{
qDebug()<<"This is CSV exapmle!\n";
}
QList<QStringList> CsvClass::ReadFromCSV(QString PathName)
{
QList<QStringList> retList;
qDebug().noquote()<<"Original CSV name:"<<PathName.toStdString().c_str();
//指定为GBK
QTextCodec *codec = QTextCodec::codecForName("GBK"); //hwm add
QFile file(PathName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return retList;
while (!file.atEnd()) {
//QByteArray line = file.readLine();
//QString line = file.readLine(); //hwm del
QString line = codec->toUnicode(file.readLine());//hwm add
line.replace('\n',"");//delete \n symbol
QStringList lineList = line.split(',');
retList.append(lineList);
qDebug().noquote()<<lineList;
}
file.close();
return retList;
}
void CsvClass::SaveToCSV(QList<QStringList> CsvList,QString PathName)
{
qDebug().noquote()<<"\nDestination CSV name:"<<PathName;
for(int i=0;i<CsvList.size();i++)
{
QString tmpStr;
QStringList strList = CsvList.at(i);
for(int j=0;j<strList.size();j++){
tmpStr.append(strList.at(j));
if(j<(strList.size()-1)){
tmpStr.append(',');
}else{
tmpStr.append('\n');
}
}
qDebug().noquote()<<i<<'\t'<<tmpStr;
QFile file(PathName);
if(!file.open(QIODevice::WriteOnly| QIODevice::Text | QIODevice::Append)){
qDebug()<<"Open "<<PathName<<" Error!";
return;
}else{
QTextCodec *codec = QTextCodec::codecForName("GBK"); //设置为ANSI编码 hwm add
QTextStream in(&file);
in.setCodec(codec);//hwm add
in<<tmpStr;
file.close();
}
}
}
使用的时候可以在mainwindow中或者在代码区都可以
CsvClass *CsvObj = new CsvClass();
QList<QStringList> retList = CsvObj->ReadFromCSV("Data_temp.csv");
CsvObj->SaveToCSV(retList,"Data_w_temp.csv");//测试使用
我的mainwin下是这样:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "chinamapwidget.h"
#include "csvclass.h"
#include <QMainWindow>
#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
ChinaMapWidget* chinaMapWidget;
CsvClass *CsvObj;
QList<QStringList> retList;
QTextToSpeech* tts;
public slots:
void PrintText(QByteArray);//定义槽函数
private slots:
void on_pushButton_tts_clicked();
void on_pushButton_tts_start_clicked();
private:
Ui::MainWindow* ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
...
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
chinaMapWidget = new ChinaMapWidget(this);
//setCentralWidget(chinaMapWidget);
chinaMapWidget->setGeometry(QRect(0, 0, 580, 480));
chinaMapWidget->show();
//ui->widget_map->
chinaMapWidget->loadJson(QApplication::applicationDirPath() + "/datas.json");
connect(chinaMapWidget, SIGNAL(showResult(QByteArray)), this, SLOT(PrintText(QByteArray)), Qt::QueuedConnection);
CsvObj = new CsvClass();
retList = CsvObj->ReadFromCSV("China_province.csv");
CsvObj->SaveToCSV(retList,"Data_w.csv");
// 创建TTS语音对象(尽量用new创建)
tts= new QTextToSpeech(this);
// 发出声音
tts->say("你好!欢迎使用全国地理信息学习系统");
}
...