Hi
I strugle with AppImage and I have some success (AppImage is created and my app can run from it).
But I have strange problem: When I start my app (writen with C++ and Qt) I setup all dirs suposed to use by my app (this is due to operating systems differences). Especially I have dir to images. I setup these dirs simply related to my exe (argv[0] main function parameter). Problem is that I have message like this:
Error: Exception occurs!
File: /home/szyk/!-EnergoKod/!-Libs/EnergoKodEditor1/Src/LineNumberArea.cpp, in line: 68, in function: loadSettings
Error code: 6, Error message: Bookmark image not found! Missing file: /home/szyk/!-EnergoKod/Textprofan2/Installers/Linux/share/Textprofan2-x86_64.AppImage/Images/bookmark.svg
Module: Energo Kod Editor 1 library
Code: 6
In this case I suppose (on Linux) my exe is located in /usr/bin
and images are in /usr/share/Textprofan2/Images
I want to write my app properly so I want to know how should I setup my internaly used dirs.
So my question is:
How shoud I access my app resources? In case it is working inside AppImage package. It require some special hanling? Or I lame in my code?
Best regards
Szyk Cech
Hi and welcome.
You could use QCoreApplication::applicationDirPath()
and construct a relative path to your resources from there.
http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath
1 Like
Thank you! You are avesome expert!!!
Your solution cover cases:
- when I run my app from AppImage package
- when I run my app from sources (from
/usr/local/bin
dir)
- when user use custom command e.g.:
cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Release
But it does not cover one importand case:
4) compile from Ide in debug mode and run without install. In this case relay on exe position is useless.
Do you have any advices how to manage 4. case?
I can see two solutions:
I. Add function which search some default directories (e.g.: /usr/share
, /usr/local/share
, ../share
)
II. Create ../share/MyApp
dir above build dir
But I don’t know what is “recomended” way…
But it does not cover one importand case:
4) compile from Ide in debug mode and run without install. In this case relay on exe position is useless.
I found the solution in Qt: Let assume we have /usr/local/share/APP_NAME/Images
dir. In order to access it properly we have to set our internal directories with QStandardPaths::locate
function. This function search ~/.local/share
, /usr/local/share
, /usr/share
dirs in order to find dirs or files given by parameter. The function is so kind that it properly handle subdirs like: gProgramName + '/' + QStringLiteral("Images")
. So if you want to have reliable access to the images try some thing like this:
QString Application::findSharedPath(QString aSubPath)
{
if(aSubPath[0] == '/')
aSubPath = aSubPath.mid(1);
QString lInstallBasePath(gPath(QCoreApplication::applicationDirPath()));
QString lRelatedSharePath = QStringLiteral("%1/share/%2").arg(lInstallBasePath).arg(gBaseName(QCoreApplication::applicationFilePath()));
QString lPath = lRelatedSharePath + '/' + aSubPath;
if(!QFileInfo::exists(lPath))
lPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, gProgramName + '/' + aSubPath, QStandardPaths::LocateDirectory);
if(!QFileInfo::exists(lPath))
{
lPath = lRelatedSharePath + '/' + aSubPath;
NOTIFY_WARNING(tr("Path not found! Missing path: %1").arg(lPath), gComponent, eErrorCode::eFileDoesNotExists);
}
return lPath;
}
1 Like
Hello, to help you further It would be very helpful if you could post a link to your source code, ideally on GitHub or GitLab. Thank you very much.