在windows开发中,中文桌面版本的默认字符集是GBK2312,如果要显示文件路径的时候,路径中带中文,如果不经过转码处理,就会导致找不到文件。解决方法中的一个就是使用QTextCodec。
QString qslPath = QFileDialog::getOpenFileName(this, "D:/", "*.*");
ui.m_pLbPath->setText(qslPath);
QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题
std::string name = code->fromUnicode(qslPath).data();
FILE* fp = fopen(name.c_str(), "r");//如果不转换,就会找不到文件
if (fp == nullptr)
{
QMessageBox::warning(this, "提醒", qslPath);
}
fclose(fp);