QDir absolutePath on Mac - c++

Im getting two different paths when i run the same build within Qt Creator and when I double click on it from the Finder on a Mac.
Here is my code:
QDir dir = QDir::currentPath();
dir.cdUp();
dir.cdUp();
dir.cdUp();
QString rootPath = dir.absolutePath();
When I run it (debug) mode in Qt Creator my path is:
/Users/myuser/Projects/AppName/build/mac
When I double click on the file that is located on
/Users/myyser/Projects/AppName/build/mac from finder it returns
/ only.
Why would I get two different paths?
Version: Qt5.2.1
Update
Seems like its a bug from reading the following URLhttp://qt-project.org/forums/viewthread/34019

Why would I get two different paths?
As they write in the thread you linked, QDir::currentPath() does not necessarily returns the application directory. It will return the path from wherever the application is run, which will be different than the application directory when running the application from the command line, or even from "start menu" alike places and so on.
If you wish to deal with the application directory to navigate from there, you would need to use the following method instead:
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).
The last sentence even clarifies the Mac OS X case.

The current directory can be anything, it solely depends on how your process is launched. What you've shown so far is that Qt Creator and Finder start the process with different current directory, that's all.
The only use for currentPath without setting it first, that I can think of, is in command line / console applications. Why do you think you need to use it? To what end?

Related

How can I open a pdf with a relative path using the default application in a Qt program

I am trying to open a pdf manual for some hardware from within my application. It will be triggered from the help menu. I can't seem to get my application to open a file from a relative path using the OS default application. I found
QDesktopServices::openUrl(QUrl("file:///home/folder/Manual.pdf"));
referenced on the Qt forums and this works for me except I am going to install my software on another computer and the absolute path won't be the same. I'd like to install the manual next to my application and open it with something like
QDesktopServices::openUrl(QUrl("file:///Manual.pdf"));
but this fails to open with
ShellExecute 'file:///Manual.pdf' failed (error 2).
Has anyone done this before?
You can retrieve the absolute path with
QString a = QFileInfo("Manual.pdf").absoluteFilePath();
then pass it to QUrl, this way:
QUrl url = QUrl::fromLocalFile(a);
If Manual.pdf is in the folder of the executable, use:
QDesktopServices::openUrl(QUrl("file:///" + QApplication::applicationDirPath() + "/Manual.pdf"));
If you later decide to put the pdf in a dedicated sub-folder, e.g. doc, in the code above replace /Manual.pdf with /doc/Manual.pdf.

Qt: Telling a program to read other files from the same directory as it's placed in

I have a program which is going to take a few different files as input.
All I am going to know is that the files are going to be in the same folder as my program (and I know their names).
Is there a way to write a path to a file knowing only its name and that it will be in the same folder as the main program?
If you are sure the files are in the same folder of your program you could use:
QCoreApplication::applicationFilePath()
You are looking for these from QCoreApplication.
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled)
Warning: On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
and
QString QCoreApplication::applicationFilePath() [static]
Returns the file path of the application executable.
For example, if you have installed Qt in the /usr/local/qt directory, and you run the regexp example, this function will return "/usr/local/qt/examples/tools/regexp/regexp".
Warning: On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
Depending on your exact use case, you use one of them, probably the former if you wish to get the executable path + your other files appended.

qt creator can not load icon

I am a beginer of QT, I tried to add an action to a toolbar and I wrote as follows:
toolbar->addAction(QIcon("/icons/new.png"), "New File");
However, the image can not be loaded. Is that related to my debug path? I can see the button, but no images.
those codes are in test.cpp and my icons folder is in the same path with test.cpp
Paths are interesting.
If you are on linux, you just specified that the root of the drive has a folder called icons, and inside it it has a file called new.png.
If you were using that kind of a path on a website, it would chop off everything to the left besides the domain and subdomain names.
example.com/path/to/some/folder/index.html, processes link to /icons/new.png, and you end up at: example.com/icons/new.png.
The best way to handle paths correctly is to use common notations for relative paths (in most cases... in some cases, absolute paths make sense).
./ means the folder that I am currently in aka the working directory.
../ means the folder above me.
A leading / means the highest folder possible or the root folder, on Unix systems. It is also akin to giving an absolute path for a file.
No leading . or .. or / means the same as ./, or from the working directory.
And there are even more rules about this. See the wiki entry:
http://en.wikipedia.org/wiki/Path_(computing)
In Qt there is also the resource system, that embeds files into the exe itself and can give you a harder-to-change image or graphic on your program.
The notation to access this is:
:/ means the root of the qresource system.
And if you do decide there is a reason to use a backslash, be sure to escape it. Normally Qt will take any input with backslashes and convert it for you on the fly to forward slashes.
So to double check that the file is there, use QFile file("icons/new.png"); followed by if(!file.exists()){ qDebug() << "File is not found!" << file.fileName(); }
Sometimes I find it helpful to see where my program is when this is happening. Either using system("dir"); or qDebug() << QDir::currentPath();
http://doc.qt.io/qt-5.5/qdir.html#currentPath
You can also see what your initial working directory is by looking at your project properties for the project in Qt Creator under:
Projects (tab) > Run (tab) > Run > Working directory:
Usually it is the root of where your source code is and where your .pro file is located.
Hope that helps.
If you are using Qt resources then you need a colon:
toolbar->addAction(QIcon(":/icons/new.png"), "New File");`
if you don't use resources then yuo should use relative path. Remember that Qt Creator uses by default "build in different place" so you must make sure that icon is deployed in respective directory.

Open txt file with default program

In my program, I have a button that I want to open a text file in a relative directory. I'm using QDesktopServices like this:
QDesktopServices::openUrl(QUrl::fromLocalFile("file:///stuff/block_settings.txt"));
When the button is pressed, nothing happens.
The file is in a folder named "stuff" that resides in the same location as my .exe. It is the same directory used for all my other tasks.
What am I doing wrong?
Thanks.
The file is in a folder named "stuff" that resides in the same location as my .exe. It is the same directory used for all my other tasks. What am I doing wrong?
Seems like your full path is an overcomplication. I would suggest to use this intead:
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).
Warning: On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
So, you would be writing this code:
QDesktopServices::openUrl(QString("%1/stuff/block_settings.txt")
.arg(QCoreApplication::applicationDirPath()));
I fixed the issue. Changed to:
QDesktopServices::openUrl(QUrl("file:stuff\\block_settings.txt"));
Not sure how that works because I don't see that configuration on any tutorial anywhere but w/e

Launch an executable from the same dir as a Qt app when white spaces in path

I would like to launch an executable when pushing a button in my Qt app.
This .exe is always located in the same directory as the Qt app itself.
Sometimes there are white spaces in the path to this directory. This seems to prevent the .exe from starting.
Here is my code (that doesn't seem to work):
QString path = QCoreApplication::applicationDirPath ();
path.append("/executable.exe");
QProcess process;
process.execute(path);
I don't know if it is possible to start the .exe without showing a command prompt first.
When the .exe is running I have to close the Qt app, while the .exe keeps running.
execute(QString) uses a single string for both executable path and arguments. Without proper quoting, C:\A path with spaces\foo.exe will be interpreted as c:\A as executable and path, with spaces\foo.exe as arguments.
To avoid this, use the overload execute(QString, QStringList) that takes the arguments as separate string list, even if you don't want to pass arguments at all:
QProcess::execute(path, QStringList());
This does the right thing and doesn't require any quoting from your side.