在使用Qt的项目中,带有中文路径的文件,在UI界面可以正常显示文件路径,但是在传到算法中时文件路径时乱码,导致在读取文件时,因找不到文件导致程序崩溃。下面是错误代码的地方:
QLineEdit* m_lineTxtPath;
txtToModel(m_tContext, m_lineTxtPath->text().toStdString(), modelFile.toStdString())
这段代码在路径是英文时运行时正常的,当路径中有中文时,m_lineTxtPath->text().toStdString(),modelFile.toStdString(),这两个就是乱码;查找资料后,用下面的方式可以完美解决:
QLineEdit* m_lineTxtPath;
// txtToModel(m_tContext, m_lineTxtPath->text().toStdString(), modelFile.toStdString())
txtToModel(m_tContext, std::string(m_lineTxtPath->text().toLocal8Bit()), std::string(modelFile.toLocal8Bit()));
更改后运行正常。