我可以使用applicationDirPath()来访问更高目录级别的资源吗? [英] Can I use applicationDirPath() to access resources at a higher directory level?

查看:943
本文介绍了我可以使用applicationDirPath()来访问更高目录级别的资源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开发的应用程序(使用C ++和Qt)中,我使用 QApplication :: applicationDirPath()访问一些资源,关于应用程序的路径。

$作为一个例子,由于我想从应用程序打开一个HTML手册,我这样做:

 

code> void MainWindow :: on_actionHelp_triggered()
{
QString link = QApplication :: applicationDirPath()+/Guide/guide.html;
bool r = QDesktopServices :: openUrl(QUrl :: fromLocalFile(link));
}

如果项目结构显示路径ProjectName / bin / Release /Guide/guide.html(因为.exe文件在ProjectName / bin / Release / AppName.exe中)。



但是,到更高级目录级资源?例如,我希望我的HTML文件在ProjectName / data / Guide / guide.html。但是这种方式,似乎不可能以我的行为方式组成路径。



编辑:@ olive的评论后,我想澄清一件事:
../'?
因为它不会在Visual Studio中工作,在那里我大规模启动应用程序来测试它。从VS,事实上,我会使用../data/Guide/guide.html,当从外面,我必须做../../data/Guide/guide.html 。



这就是为什么QApplication :: applicationDirPath()存在的原因。但是,我不是一个专家,所以不要责怪我,并纠正我的任何最终的错误。

解决方案

使用 .. QApplication :: applicationDirPath()+/../../ data / Guide / guide.html是完全有效的路径!



当然还有另一个问题。当安装应用程序时,相对路径可能会再次不同。您需要在visual studio中配置路径,以使相对路径在开发过程中和部署后都起作用,或者您需要检测布局。


In the application that I am developing (using C++ and Qt), I am using QApplication::applicationDirPath() to access some resources, with respect of the application's path.

As an example, since I want to open a HTML manual from the application, I act this way:

void MainWindow::on_actionHelp_triggered()
{
    QString link = QApplication::applicationDirPath() + "/Guide/guide.html";
    bool r = QDesktopServices::openUrl(QUrl::fromLocalFile(link));
}

This snippet works if the project's structure presents the path "ProjectName/bin/Release/Guide/guide.html" (since the .exe file is in "ProjectName/bin/Release/AppName.exe").

But what can I do to refer to a higher-directory-level resource? As an example, I wish my HTML file to be in "ProjectName/data/Guide/guide.html". But this way, it seems not possible to compose the path in the way I'm acting.

EDIT: After @olive's comment, I wish to clarify a thing: "Why am I not using '../'?" Because it won't work from Visual Studio, where I am massively launch the application to test it. From VS, in fact, I shall use "../data/Guide/guide.html", when "from the outside", I'd have to do "../../data/Guide/guide.html".

That's why (I think) QApplication::applicationDirPath() exists. However, I am not an expert, so don't blame me and correct any eventual mistake of mine, please!

解决方案

Just use ... QApplication::applicationDirPath() + "/../../data/Guide/guide.html" is perfectly valid path!

Of course there is another problem. When the application is installed, the relative path will probably be different again. You either need to configure the paths in visual studio so that the relative path works both during development and after deployment, or you need to detect the layout.

这篇关于我可以使用applicationDirPath()来访问更高目录级别的资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆