Tuesday, 27 August 2013

Can I use applicationDirPath() to access resources at a higher directory level?

Can I use applicationDirPath() to access resources at a higher directory
level?

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!

No comments:

Post a Comment