windows下Qt开发中识别中文路径
QT中打开文件,当文件路径存在中文名是会出现 乱码或者“???”的情况
网上常见的一种方法是使用:
QTextCodec *code = QTextCodec::codecForName("gb18030");
std::string name = code->fromUnicode(pathname).data(); // pathname 为QString的中文路径名
这类方法是使用C++中的std::string来识别
但是此时的name类型为string,可能还需要转换到QString然后使用QFile file(name)这种方法打开。
另一种方法是在Qt文件main函数加入`
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//QT中获取中文路径
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
}
或者是
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//QT中获取中文路径
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF_8"));
目前试了一下,GB2312和 UTF-8都可以。具体有什么区别后续可能会补充。