QFile::exists问题,包含中文路径

tingsking18 2010-01-26 08:21:41
c:/测试/test/test.cpp这个文件是存在的,但是老是返回不存在,看到cuteqt的blog上面有一个类似的东西,看了半天也买看出个456来。
请高人指点。

#include <QApplication>
#include <QPushButton>
#include <QFile>
#include <QMessageBox>
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
if(QFile::exists("c:/测试/test/test.cpp"))
QMessageBox::information(0, "Message from Qt", "exist!");
else
QMessageBox::information(0, "Message from Qt", "not exist!");
return 0;
}
...全文
2693 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
aoxuehan0424 2010-02-09
  • 打赏
  • 举报
回复

我好像有点儿懂你的意思了!

我之前说的,不是你问到那个问题!!!
我一般qmake 的时候,都是先cd到源文件所在的目录,所以没有碰到过这种问题。
aoxuehan0424 2010-02-09
  • 打赏
  • 举报
回复
我就是在ubuntu下 把这个代码放到一个 test.cpp文件里面。
然后:
qmake -project
qmake test.pro
make

然后双击生成的test文件
OK。

我没用QtCreater

而且我以前一个windows下的项目,也用到了这个。我也是qmake生成的vc的项目文件:
qmake -project
qmake ***.pro
qmake -tp vc ***.pro


btw: 还是我说的那句话:
main函数里面有没?
main函数有用qtextstream处理的话,其他地方要用到汉字,就不需要加tr(""),否则,用到汉字的地方,都要加tr
这句话好像是书上的。 the c++ gui qt 这本书。
tingsking18 2010-02-09
  • 打赏
  • 举报
回复
其实问题不是在程序中使用中文的问题。程序中不需要使用中文。
是QString的编码问题。如果QString中包含中文,这个QString 的编码补正确,所以QFile::exist()就返回不了正确的结果。


引用 18 楼 tingsking18 的回复:
这个代码我也可以调试通过,我说的是qmake中。请参看2楼。
你把这段代码放到qmake的工程中试试。

btw:qmake中好像不能用QTextCodec
引用 16 楼 aoxuehan0424 的回复:LZ 我用你的代码在ubuntu下测试,也是,明明有这个文件,但是弹出框说没有,我把代码改成了这样就可以了: C/C++ code #include<QApplication> #include<QPushButton> #include<QFile> #include<QMessageBox> #include<QTextCodec> #include<QObject>int main(int argc,char* argv[]) { QTextCodec*codec= QTextCodec::codecForName("System"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); QApplication app(argc,argv);if(QFile::exists("/home/lei/桌面/code/test/1")) QMessageBox::information(0,"Message from Qt","exist!");else QMessageBox::information(0,"Message from Qt","not exist!");return0; } 加入了QTextCodec.为什么要加这个。原因就是我在上楼说到这样!
tingsking18 2010-02-09
  • 打赏
  • 举报
回复
你编译一下qmake,在qmake工程
main.cpp中的函数:
int runQMake(int argc, char **argv)

你把你的代码放替换这句if(!QFile::exists(fn)) 看看什么结果?

引用 18 楼 tingsking18 的回复:
这个代码我也可以调试通过,我说的是qmake中。请参看2楼。
你把这段代码放到qmake的工程中试试。

btw:qmake中好像不能用QTextCodec
引用 16 楼 aoxuehan0424 的回复:LZ 我用你的代码在ubuntu下测试,也是,明明有这个文件,但是弹出框说没有,我把代码改成了这样就可以了: C/C++ code #include<QApplication> #include<QPushButton> #include<QFile> #include<QMessageBox> #include<QTextCodec> #include<QObject>int main(int argc,char* argv[]) { QTextCodec*codec= QTextCodec::codecForName("System"); QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QTextCodec::setCodecForTr(codec); QApplication app(argc,argv);if(QFile::exists("/home/lei/桌面/code/test/1")) QMessageBox::information(0,"Message from Qt","exist!");else QMessageBox::information(0,"Message from Qt","not exist!");return0; } 加入了QTextCodec.为什么要加这个。原因就是我在上楼说到这样!
tingsking18 2010-02-09
  • 打赏
  • 举报
回复
这个简单的test.cpp已经调试出来了。没有问题的。
我要解决的不是这个问题。
我要解决的是qmake出现的问题:
qmake c:\测试\test\test.pro
qmake会返回can't find file c:\测试\test\test.pro

在qmake的源代码中qtextstream和QTextCodec不能引用。


#include <QApplication>
#include <QPushButton>
#include <QFile>
#include <QMessageBox>
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
QString str = "C:/测试/test/test.cpp";
str = QString::fromLocal8Bit(str.toLatin1().constData());
if(QFile::exists(str))
QMessageBox::information(0, "Message from Qt", "exist!");
else
QMessageBox::information(0, "Message from Qt", "not exist!");
return 0;
}
引用 19 楼 aoxuehan0424 的回复:
我就是在ubuntu下 把这个代码放到一个 test.cpp文件里面。
然后:
qmake -project
qmake test.pro
make

然后双击生成的test文件
OK。

我没用QtCreater

而且我以前一个windows下的项目,也用到了这个。我也是qmake生成的vc的项目文件:
qmake -project
qmake ***.pro
qmake -tp vc ***.pro


btw: 还是我说的那句话:
main函数里面有没?
main函数有用qtextstream处理的话,其他地方要用到汉字,就不需要加tr(""),否则,用到汉字的地方,都要加tr
这句话好像是书上的。 the c++ gui qt 这本书。
tingsking18 2010-02-08
  • 打赏
  • 举报
回复
这个代码我也可以调试通过,我说的是qmake中。请参看2楼。
你把这段代码放到qmake的工程中试试。

btw:qmake中好像不能用QTextCodec
[Quote=引用 16 楼 aoxuehan0424 的回复:]
LZ
我用你的代码在ubuntu下测试,也是,明明有这个文件,但是弹出框说没有,
我把代码改成了这样就可以了:
C/C++ code
#include<QApplication>
#include<QPushButton>
#include<QFile>
#include<QMessageBox>
#include<QTextCodec>
#include<QObject>int main(int argc,char* argv[])
{
QTextCodec*codec= QTextCodec::codecForName("System");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);

QApplication app(argc,argv);if(QFile::exists("/home/lei/桌面/code/test/1"))
QMessageBox::information(0,"Message from Qt","exist!");else
QMessageBox::information(0,"Message from Qt","not exist!");return0;
}

加入了QTextCodec.为什么要加这个。原因就是我在上楼说到这样!
[/Quote]
aoxuehan0424 2010-02-07
  • 打赏
  • 举报
回复
接分了哈~
aoxuehan0424 2010-02-07
  • 打赏
  • 举报
回复
LZ
我用你的代码在ubuntu下测试,也是,明明有这个文件,但是弹出框说没有,
我把代码改成了这样就可以了:

#include <QApplication>
#include <QPushButton>
#include <QFile>
#include <QMessageBox>
#include <QTextCodec>
#include <QObject>
int main(int argc,char* argv[])
{
QTextCodec *codec = QTextCodec::codecForName("System");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);

QApplication app(argc,argv);
if(QFile::exists("/home/lei/桌面/code/test/1"))
QMessageBox::information(0, "Message from Qt", "exist!");
else
QMessageBox::information(0, "Message from Qt", "not exist!");
return 0;
}


加入了QTextCodec.为什么要加这个。原因就是我在上楼说到这样!
aoxuehan0424 2010-02-07
  • 打赏
  • 举报
回复
你直接把中文路径包括在双引号之中的话,main函数里面有没有用qtextstream处理?
main函数不加这个的话,其他地方要用到汉字到话,要加tr("")

你把路径加上tr("")试试!
tingsking18 2010-02-05
  • 打赏
  • 举报
回复
QString fn = Option::fixPathToLocalOS((*pfile));
if(!QFile::exists(str1)) {
fprintf(stderr, "Cannot find file: %s.\n", fn.toLatin1().constData());
exit_val = 2;
continue;
}


这里本身就是unicode编码的。

[Quote=引用 10 楼 microsky2813 的回复:]
.toUtf8() 用unincode
[/Quote]
tingsking18 2010-02-05
  • 打赏
  • 举报
回复
同样不可以。
http://hi.csdn.net/attachment/201002/5/1207120_126536008027mM.jpg


[Quote=引用 11 楼 cuzn1024 的回复:]
QFile::exists(QString::fromLocal8Bit(std::string("c:/测试/test/test.cpp").c_str()))
这个应该是正确的。
[/Quote]
MicroSky2813 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 cuzn1024 的回复:]
QFile::exists(QString::fromLocal8Bit(std::string("c:/测试/test/test.cpp").c_str()))
这个应该是正确的。
[/Quote]

好主意,笑死我了,哈哈
  • 打赏
  • 举报
回复
QFile::exists(QString::fromLocal8Bit(std::string("c:/测试/test/test.cpp").c_str()))
这个应该是正确的。
MicroSky2813 2010-02-05
  • 打赏
  • 举报
回复
.toUtf8() 用unincode
tingsking18 2010-02-05
  • 打赏
  • 举报
回复
QFile::exist单独用是好用的。qmake不支持中文路径,我跟踪到的这个地方就是QFile::exist。
不知道为什么


[Quote=引用 8 楼 cuzn1024 的回复:]
呃,"不对"是什么意思?
QFile::exists(QString("c:/测试/test/test.cpp").toLocal8Bit());//sorry,应该是这样
[/Quote]
  • 打赏
  • 举报
回复
呃,"不对"是什么意思?
QFile::exists(QString("c:/测试/test/test.cpp").toLocal8Bit());//sorry,应该是这样
tingsking18 2010-02-05
  • 打赏
  • 举报
回复 1
这个倒是没问题,但是这段放到qmake中就是死活不对~!
[Quote=引用 6 楼 cuzn1024 的回复:]
QFile::exists(QString("c:/测试/test/test.cpp")).toLocal8Bit());
[/Quote]
  • 打赏
  • 举报
回复
QFile::exists(QString("c:/测试/test/test.cpp")).toLocal8Bit());
tingsking18 2010-02-02
  • 打赏
  • 举报
回复
顶一下
Inhibitory 2010-01-26
  • 打赏
  • 举报
回复
因为qt的环境路径中还不支持有中文或者空格的.
加载更多回复(3)

21,494

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧