修改转换Qt字符串路径为系统编码的C字符串(解决中文路径问题)实现代码

This commit is contained in:
徐海 2026-05-12 10:15:24 +08:00
parent 15d37e279e
commit cbb56426ec
2 changed files with 5 additions and 5 deletions

View File

@ -769,7 +769,7 @@ bool EnergyScaleParticleDataTask::processTask()
project_model->SetParticleEnergyDataFilename(energy_spectrum_filename); project_model->SetParticleEnergyDataFilename(energy_spectrum_filename);
} catch (const std::exception& e) { } catch (const std::exception& e) {
out.close(); out.close();
std::remove(QStrToSysPath(energy_spectrum_filename)); std::remove(QStrToSysPath(energy_spectrum_filename).c_str());
const QString& e_what = QString::fromStdString(e.what()); const QString& e_what = QString::fromStdString(e.what());
LOG_WARN(QStringLiteral(u"能谱数据处理异常:%1").arg(e_what)); LOG_WARN(QStringLiteral(u"能谱数据处理异常:%1").arg(e_what));
return false; return false;
@ -842,7 +842,7 @@ bool EnergyCountProcessTask::processTask()
project_model->SetChannelEnergyCountDataFilename(channel_num, ch_out_filename); project_model->SetChannelEnergyCountDataFilename(channel_num, ch_out_filename);
} catch (const std::exception& e) { } catch (const std::exception& e) {
ch_out.close(); ch_out.close();
std::remove(QStrToSysPath(ch_out_filename)); std::remove(QStrToSysPath(ch_out_filename).c_str());
const QString& e_what = QString::fromStdString(e.what()); const QString& e_what = QString::fromStdString(e.what());
LOG_WARN(QStringLiteral(u"%1能量计数异常:%2").arg(channel_name).arg(e_what)); LOG_WARN(QStringLiteral(u"%1能量计数异常:%2").arg(channel_name).arg(e_what));
} }

View File

@ -8,9 +8,9 @@
#include <QDebug> #include <QDebug>
// 转换Qt字符串路径为系统编码的C字符串解决中文路径问题 // 转换Qt字符串路径为系统编码的C字符串解决中文路径问题
static const char* QStrToSysPath(const QString& qstr_path) static std::string QStrToSysPath(const QString& qstr_path)
{ {
std::string sys_path; // 静态变量避免内存释放 std::string sys_path;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// Windows转为GBK编码 // Windows转为GBK编码
QTextCodec* gbkCodec = QTextCodec::codecForName("GBK"); QTextCodec* gbkCodec = QTextCodec::codecForName("GBK");
@ -20,7 +20,7 @@ static const char* QStrToSysPath(const QString& qstr_path)
// Linux/Mac转为UTF-8编码 // Linux/Mac转为UTF-8编码
sys_path = qstr_path.toUtf8().toStdString(); sys_path = qstr_path.toUtf8().toStdString();
#endif #endif
return sys_path.c_str(); return sys_path;
} }
static int ExtractNumberFromString(const QString& str) { static int ExtractNumberFromString(const QString& str) {