fix:解决中文乱码BUG。

This commit is contained in:
taynpg 2024-05-24 09:18:06 +08:00
parent 019740f550
commit 3d1fcc5dc8
6 changed files with 90 additions and 80 deletions

View File

@ -1,7 +1,7 @@
{
"files.autoSave": "onFocusChange",
"editor.fontSize": 16,
"editor.fontFamily": "'Mononoki Nerd Font Mono', 'Mononoki Nerd Font Mono', 'Mononoki Nerd Font Mono'",
"editor.fontFamily": "'IBM Plex Mono', 'IBM Plex Mono', 'MIBM Plex Mono'",
"cmake.configureOnOpen": true,
"cmake.debugConfig": {
"console": "integratedTerminal",

View File

@ -16,7 +16,7 @@ MainWidget::MainWidget(QWidget* parent) : QWidget(parent), ui(new Ui::MainWidget
{
ui->setupUi(this);
setWindowTitle(u8"OneLevelXmlOpr v1.2.13");
setWindowTitle(u8"OneLevelXmlOpr v1.2.14");
setWindowIcon(QIcon("://resource/xml.ico"));
QScreen* primaryScreen = QGuiApplication::primaryScreen();
@ -185,7 +185,7 @@ void MainWidget::generate_table_widget()
[&](QTableWidgetItem* item) { item_changed_handle(item); });
connect(tab_widget_, &QTableWidget::customContextMenuRequested, this, &MainWidget::show_custom_menu);
auto config = ini_.get_config();
auto keys = splitString(config.purpose, ",");
auto keys = CUtil::splitString(config.purpose, ",");
keys_.clear();
QStringList list;
for (const auto& item : keys) {
@ -240,6 +240,7 @@ void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles, st
tab_widget_->clearContents();
tab_widget_->setRowCount(0);
auto_add_ = true;
for (auto p = (page - 1) * g_OnePage; p < all_size && p < max_show; ++p) {
int row_cnt = tab_widget_->rowCount();
tab_widget_->insertRow(row_cnt);
@ -249,6 +250,7 @@ void MainWidget::push_content(const std::vector<tinyxml2::XMLElement*>& eles, st
cur_page_ = page;
ui->edAllPage->setText(QString::number(all_page_));
judge_btn_page();
auto_add_ = false;
}
void MainWidget::judge_btn_page()
@ -285,11 +287,9 @@ void MainWidget::read(const QString& file_path)
return;
}
auto_add_ = true;
generate_table_widget();
push_content(vec_);
current_ = vec_;
auto_add_ = false;
ui->edStatus->setText(file_path);
ui->btnRead->setEnabled(false);
@ -350,7 +350,7 @@ void MainWidget::item_changed_handle(QTableWidgetItem* item)
if (result == nullptr) {
return;
}
result->SetAttribute(keys_[col].c_str(), item->text().toLocal8Bit().constData());
result->SetAttribute(keys_[col].c_str(), item->text().toStdString().c_str());
}
void MainWidget::save()
@ -495,8 +495,8 @@ void MainWidget::insert_one_line(Element_t* ele, int row)
// wgItem->setFlags(wgItem->flags() | Qt::ItemIsUserCheckable);
// wgItem->setCheckState(Qt::Checked);
}
wgItem->setText(QString(data));
QString sda(data);
wgItem->setText(sda);
tab_widget_->setItem(row, i, wgItem);
}
}

View File

@ -2,23 +2,9 @@
#include <QMessageBox>
#include <QFileDialog>
std::vector<std::string> splitString(const std::string& input, const std::string& delimiter)
{
std::vector<std::string> tokens;
size_t pos = 0;
std::string backup = input;
std::string token;
while ((pos = backup.find(delimiter)) != std::string::npos) {
token = backup.substr(0, pos);
tokens.push_back(token);
backup.erase(0, pos + delimiter.length());
}
// Push the remaining part after the last delimiter
tokens.push_back(backup);
return tokens;
}
#ifdef _WIN32
#include <windows.h>
#endif
void CUtil::msg(QWidget* parent, const QString& content)
{
@ -42,8 +28,7 @@ bool CUtil::affirm(QWidget* parent, const QString& titile, const QString& conten
QString CUtil::select_file(QWidget* parent, const QString& info, const QString& filter)
{
QString filePath =
QFileDialog::getOpenFileName(parent, info, QDir::homePath(), filter);
QString filePath = QFileDialog::getOpenFileName(parent, info, QDir::homePath(), filter);
return filePath;
}
@ -68,3 +53,50 @@ void CUtil::sort_by_repeat(std::vector<std::string>& vec)
std::sort(vec.begin(), vec.end(), compare);
}
}
#ifdef _WIN32
std::string CUtil::utf8_to_gbk(const std::string& utf8_str)
{
// UTF-8 to Wide Char (UTF-16)
int wide_char_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_len == 0) {
return "";
}
std::wstring wide_str(wide_char_len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, &wide_str[0], wide_char_len);
// Wide Char (UTF-16) to GBK
int gbk_len = WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (gbk_len == 0) {
return "";
}
std::string gbk_str(gbk_len, 0);
WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, &gbk_str[0], gbk_len, nullptr, nullptr);
return gbk_str;
}
std::vector<std::string> CUtil::splitString(const std::string& input, const std::string& delimiter)
{
std::vector<std::string> tokens;
size_t pos = 0;
std::string backup = input;
std::string token;
while ((pos = backup.find(delimiter)) != std::string::npos) {
token = backup.substr(0, pos);
tokens.push_back(token);
backup.erase(0, pos + delimiter.length());
}
// Push the remaining part after the last delimiter
tokens.push_back(backup);
return tokens;
}
#else
std::string CUtil::utf8_to_gbk(const std::string& utf8_str)
{
return utf8_str;
}
#endif

View File

@ -12,18 +12,21 @@ struct OprBase {
std::string xml_path{};
};
std::vector<std::string> splitString(const std::string& input, const std::string& delimiter);
class CUtil
{
public:
CUtil() = default;
~CUtil() = default;
public:
static void msg(QWidget* parent, const QString& content);
static bool affirm(QWidget* parent, const QString& titile, const QString& content);
static QString select_file(QWidget* parent, const QString& info, const QString& filter);
static void sort_by_repeat(std::vector<std::string>& vec);
public:
static std::string utf8_to_gbk(const std::string& utf8_str);
static std::vector<std::string> splitString(const std::string& input, const std::string& delimiter);
};
#endif

View File

@ -24,6 +24,7 @@ bool ConfigIni::set_xml_path(const std::string& path)
if (ini_.IsEmpty()) {
return false;
}
ini_.SetValue("Basic", "xml_path", path.c_str());
if (ini_.SaveFile(ini_path_.c_str()) != SI_OK) {
return false;

View File

@ -1,32 +1,6 @@
#include "xml_opr.h"
#include <filesystem>
#ifdef _WIN32
#include <windows.h>
std::string utf8_to_gbk(const std::string& utf8_str)
{
// UTF-8 to Wide Char (UTF-16)
int wide_char_len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
if (wide_char_len == 0) {
throw std::runtime_error("Failed to convert UTF-8 to wide char");
}
std::wstring wide_str(wide_char_len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, &wide_str[0], wide_char_len);
// Wide Char (UTF-16) to GBK
int gbk_len = WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (gbk_len == 0) {
throw std::runtime_error("Failed to convert wide char to GBK");
}
std::string gbk_str(gbk_len, 0);
WideCharToMultiByte(CP_ACP, 0, wide_str.c_str(), -1, &gbk_str[0], gbk_len, nullptr, nullptr);
return gbk_str;
}
#endif
namespace fs = std::filesystem;
CXmlOpr::CXmlOpr() = default;
@ -35,7 +9,7 @@ CXmlOpr::~CXmlOpr() = default;
bool CXmlOpr::open(const std::string& xml_path)
{
#ifdef _WIN32
if (doc_.LoadFile(utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
if (doc_.LoadFile(CUtil::utf8_to_gbk(xml_path).c_str()) != tinyxml2::XML_SUCCESS) {
return false;
}
#else
@ -81,7 +55,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
std::string next_node{};
std::string node_path = opr_base_.node_path;
keys_.clear();
auto keys = splitString(opr_base_.purpose, ",");
auto keys = CUtil::splitString(opr_base_.purpose, ",");
for (const auto& item : keys) {
if (item.empty()) {
continue;
@ -89,7 +63,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
keys_.push_back(item);
}
auto nodes = splitString(opr_base_.node_path, "/");
auto nodes = CUtil::splitString(opr_base_.node_path, "/");
for (const auto& item : nodes) {
if (item.empty()) {