首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

Qt中调用当前工程中的文件,为何出现如此怪异有关问题

2013-03-06 
Qt中调用当前工程中的文件,为何出现如此怪异问题?//代码:#include QApplication#include QtGuiint mai

Qt中调用当前工程中的文件,为何出现如此怪异问题?
//代码:
#include <QApplication>
#include <QtGui>

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

    QString str = "code/asd.doc";
    if(!QDesktopServices::openUrl(QUrl::fromLocalFile(str))) {
        QMessageBox::about(0, "asd",
                           "asdf");
    }

    return app.exec();
}

//目录
|-test-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK____
  |-code
    |-asd.doc
  |-debug
    |-main.o
    |-test.exe
  |-release
  |-asd.doc
  |-Makefile
  |-Makefile.Debug
  |-Makefile.Release

将代码中的str改为 "asd.doc",程序运行成功,调用了该文档!
但是str为"code/asd.doc",程序就失败,没有调用成功!
我不知道哪里错了,我以前弄这个的时候,就是这么调用的,可今天就不对了,

//以前做的笔记如下:
QDesktopServices::openUrl(QUrl::fromLocalFile("test/test.dsw"));//同时可利用其还回值判断是否成功打开。

文件地址:
绝对路径法:
E:\\aa\\aa\\aa.exe

相对路劲(当前工程里):
aa/aa/aa.dsw

如果您说相对路劲应该是和.exe同目录下,那么我告诉您,
我将code文件夹放入了debug文件中和.exe同目录,但是运行还是错误,不能成功调用!

在线的等前辈的解答啊!
我以前是在XP下弄的,现在是W7,难道于系统有关么? 应该不是哦!
[解决办法]
code/asd.doc改成./code/asd.doc呢

用QFileinfo info("code/asd.doc");
调用其boolexists () const
和 QStringabsoluteFilePath () const
两个方法,看能否正常检查出该文件来
[解决办法]

引用:
以前在XP的系统上可以的,现在换W7了,难道是W7的原因么?
求指教啊!
求顶啊!
求顶啊!
求顶啊!


看你的描述 感觉像是权限问题的设置造成的
QFileInfo里面有很多方法可以调用
比如
bool QFileInfo::isExecutable () const
Returns true if the file is executable; otherwise returns false.

See also isReadable(), isWritable(), and permission().
再比如用permission检查是否有执行权限
bool QFileInfo::permission ( QFile::Permissions permissions ) const
Tests for file permissions. The permissions argument can be several flags of type QFile::Permissions OR-ed together to check for permission combinations.

On systems where files do not have permissions this function always returns true.

Example:

 QFileInfo fi("/tmp/archive.tar.gz");
 if (fi.permission(QFile::WriteUser 
[解决办法]
 QFile::ReadGroup))
     qWarning("I can change the file; my group can read the file");
 if (fi.permission(QFile::WriteGroup 
[解决办法]
 QFile::WriteOther))
     qWarning("The group or others can change the file");


bool QFileInfo::isExecutable () const
Returns true if the file is executable; otherwise returns false.



See also isReadable(), isWritable(), and permission().

感觉应该是你要启动的那个.exe的权限问题 

[解决办法]
http://www.qtcentre.org/threads/52236-SOLVED-QDesktopServices-openUrl()-not-working-on-a-Win7-machine

你看上面这个帖子的说明 貌似在win7下 要用绝对路径
ok.. I got the occasion to try back my collegue pc.. I just keep it posting any how in case someone has the same issue.
For some strange reasons on Ubuntu and Windows XP the relative position was fine (foth in the Qt Creator and in the installed application) but in Windows 7 it seems it wants the full path...
[解决办法]
那你用QFileinfo检查下权限
看对于相对路径和绝对路径 是否有不同的permission
[解决办法]

引用:
引用:引用:以前在XP的系统上可以的,现在换W7了,难道是W7的原因么?
求指教啊!
求顶啊!
求顶啊!
求顶啊!

看你的描述 感觉像是权限问题的设置造成的
QFileInfo里面有很多方法可以调用
比如
bool QFileInfo::isExecutable () const
……


我手边没win7,暂时只能给点建议
读写执行很正常啊,在linux,unix体系上一直是3个权限位,分别为rwx,对应4,2,1,有读写权限跟有执行权限是2回事
我这里不说win7,因为在vista之前的windows在权限管理上一直做的很弱
可能在win7这方面做了加强
建议做如下的测试
    给出绝对路径后,看其绝对路径(除文件名外)是否有执行权限,比如你的文件是  C:/opt/Destktop/hello.exe,则你测试下C:/opt/Destktop/这个目录(用QFileinfo)测试下是否有执行权限
    再进入到C:/opt目录下 ,用QFileinfo测试下 Desktop和./Desktop是否都有执行权限。
    unix规定是对于可访问的文件,要求对其路径上的任意目录都应有执行权限, 不知道win7是否向linux看齐了


btw qt底层也是用的平台相关的那一套,估计是跟windows7的一些权限设置相关

热点排行