How to find the path of the file that launches the Qt application from explorer
-
const QStringList args(QCoreApplication::instance()->arguments()); qDebug() << "File path:" << args.at(1);
That should work.
Edit: Fixed at(0) to at(1)
-
@sierdzio , @QtVik
UsingQCoreApplication::instance()->arguments()[0]
is a most unreliable way to obtain a program's path. It may work reliably from Windows Explorer, I don't know, but it can be set to any string the launcher desires and/or can contain the right name but no path.I would suggest you should use
QCoreApplication::applicationFilePath()
, which I read from the docs as making an attempt to "get it right", and hopefully will be implemented using the correct Windows native OS call. You may failover toarguments()[0]
if you wish. -
@JNBarchan the OP is not asking for application file path. They are asking for path to file which has been double clicked on in the explorer.
-
@sierdzio
Oh, maybe you interpret the question better than I, then!But then I don't understand your answer, which has 2 upvotes? From the docs (http://doc.qt.io/qt-5/qcoreapplication.html#arguments):
Usually arguments().at(0) is the program name,
Are you saying that under Windows argument #0 is the "name of the program", and if run from Explorer it will be the full path to the
.dat
file, not the name/path of his Qt application? So an application has to know whether it was invoked from Explorer or not in order to know what its argv[0] will represent?? I don't run Qt under Windows, but this does not sound right to me. -
@JNBarchan
Docs do mention it can be different
"...GetCommandLine(). As a result of this, the string given by arguments().at(0) might not be the program name on Windows, depending on how the application was started."
So maybe starting from explorer is one of those cases :)
Update:
Nope, assign .rip to my own exe shows appname in (0) and param in (1)
-
Yes,
QCoreApplication::instance()->arguments()[0]
will always refer to the "program", but may be just the name instead of a full path, and shouldn't be relied on anyway, as I wrote earlier.So
[0]
is never to be used. I suspect @sierdzio was intendingarguments()[1]
maybe.However, while this may work for the OP's situation, other readers should be aware of the following. You can never be sure "which filename was clicked on in Explorer to launch application" in Windows (I suspect Linux desktops too).
When you make a "File Association" under Windows, it creates an entry in the Registry. IIRC, this is/can be a "command-line", in which the file being double-clicked on is referenced by something like
%1
. Although the default may be that this is the first argument (arguments()[1]
), plenty of programs have preceding command-line options in their command in the registry, like-a -b "%1"
(or a/print %1
for printing, for example). In this case argument 1 would be-a
, not what the OP wants.Only your program knows where it expects Explorer to pass the file path of the file clicked on the command-line, depending on option processing.
If you want to guess, the last rather then the first argument is probably advisable, e.g.
arguments()[arguments().length - 1]
. But you can't be sure. -
@JNBarchan said in How to find the path of the file that launches the Qt application from explorer:
So [0] is never to be used. I suspect @sierdzio was intending arguments()[1] maybe.
Yes, my bad, sorry! I've corrected the post.
-
@QtVik said in How to find the path of the file that launches the Qt application from explorer:
@sierdzio @JNBarchan @mrjj :
But this line is causing an assertion failure "index out of range" in qlist.h line#510
qDebug() << "File path:" << args.at(1);Try printing all args:
qDebug() << "File path:" << args;
This will show you all arguments (apart from ones consumed by Qt).
-
@sierdzio
Actually I want path of the file which is invoking my application not the path of my application.exe.Example:
I have a file named "sample.dat" in "C:\Meas folder" and Qt application in "C:\QApplication\Qapp.exe"
Now if I double click on the "sample.dat " I should be able to find this path (C:\Meas folder) through my Qt application ie. Qapp.exeThanks
-
@QtVik said in How to find the path of the file that launches the Qt application from explorer:
Actually I want path of the file which is invoking my application not the path of my application.exe.
I know, that's what we are trying to help you with.
Actually, apart from using QCoreApplication::arguments(), you may also consider using QDir::currentPath().
-
@sierdzio said in How to find the path of the file that launches the Qt application from explorer:
you may also consider using QDir::currentPath().
Do you have any evidence/knowledge that launching by double-clicking from Windows Explorer sets the application's current directory to that of the file double-clicked on??
-
@JNBarchan said in How to find the path of the file that launches the Qt application from explorer:
@sierdzio said in How to find the path of the file that launches the Qt application from explorer:
you may also consider using QDir::currentPath().
Do you have any evidence/knowledge that launching by double-clicking from Windows Explorer sets the application's current directory to that of the file double-clicked on??
Nope, it's just a guess.
-
@JNBarchan
Following are the values for const QStringList args(QApplication::instance()->arguments()), argc ,argv:
1] argc value is 1
2] argv value is the application path .
3] args.at(0) value is also application path .
4] args.at(1) -> crash the application .The above values are shown when I double click on the application directly.
But this is not what i want !
I want path of the file that is trying to invoke my application from explorer.
Example: from the previous post it should be "C:\Meas_folder\sample.dat"Note: I have set my Qapp.exe as default application to open for sample.dat
Thanks
-
@QtVik Please provide same information for the case you are interested in! It doesn't help at all if you double click your app and then post the results here: this is not the use case you're interested in, right?
So, open you app from explorer with a data file and post the content of QApplication::instance()->arguments() here..."Note: I have set my Qapp.exe as default application to open for sample.dat" - then it should be enough to double click on sample.dat and post the output of QApplication::instance() here.