当前位置: 代码迷 >> QT开发 >> QFileDialog 跟fopen 联合使用不正常
  详细解决方案

QFileDialog 跟fopen 联合使用不正常

热度:120   发布时间:2016-04-25 03:33:48.0
QFileDialog 和fopen 联合使用不正常
我用QFileDialog 得到一个QString的路径,我用fopen打开这个路径不能正常打开,tostdstring,data,toutf8,这些都试过了,大家有没有遇到类似的情况。我用的是Qt5,请不要将Qt4里面那些弃用的指定编码格式的函数贴在上面。我在网上搜了一大堆都是这些。
Qt 编码

------解决方案--------------------
replace("/","\\");
------解决方案--------------------
你先写死一个路径试试
------解决方案--------------------
你首先得说明自己代码和字符串是什么编码,不要盲目尝试,你应该转为windows系统编码,用QString::fromlLocal8Bit()等from  shi shi
------解决方案--------------------
你贴出你的代码看看,这样才好帮你
------解决方案--------------------
str.toStdString().data(),这个试试,我想应该是你的file这个串解析不对,如果这还不行的话,

你试着弄过文件测试下,比如在c:\\test.txt建立一个文件,然后

fopen("c:\\test.txt", 'w');如果这能成功,肯定是解析得问题。
------解决方案--------------------
QString file;
QByteArray nativeFilePath = QByteArray((const char *)file.utf16(), file.size() * 2 + 1);

FILE* f= fopen((const wchar_t*)nativeFilePath.constData(),'w');

如果还不行,换种方法:

int main(int argc, char**argv)
{
    QApplication app(argc,argv);

    QString str = QFileDialog::getOpenFileName(NULL,
                                               "Open G File",
                            "",
                            "G File (*.rf)"
                            );
    QFile file(str);
    if (file.open(QIODevice::ReadWrite))
        qDebug() << "ok";
    else
        qDebug() << "fail";
    return app.exec();
}
这种肯定行
------解决方案--------------------
加一句应该就可以了
setlocale(LC_CTYPE, "chs")
  相关解决方案