Beginner: Getting File Path from User Opened File on Launch (Windows)
-
Hi All,
I am a QtN00b (hence the user name) with a question on what I think should seem obvious but I can't seem to find the answer. Here goes:
I am writing a very simple Desktop Widget application that takes a file path/name from QtFileDialog::getOpenFileName and stores it to a QString. It then opens that file read only based on the QString and reads the first three bytes to a QByteArray for comparison to display some UI Labels. This all seems to work wonderfully well. I would like however to be able to have my end user have the ability to open my widget by either using the "Open With..." context menu or dragging their file onto the .exe (or shortcut) in Windows Explorer and have that file path already passed to the fileName string so it can do its work to display the appropriate info without having to have them use the QPushButton that calls the QtFileDialog.
I came across this article in the wiki: "Assigning a File Type to an Application on Windows":http://qt-project.org/wiki/Assigning_a_file_type_to_an_Application_on_Windows but I wasn't sure if this would still apply to a widget based application. If it does, could someone be kind enough to enlighten me on where I should implement it in my main.cpp file? (or if it should go somewhere else)
I am sorry if this topic has been covered, I did try to search and wasn't coming up with any relevant hits. If the wiki article isn't where I should be looking or if there is a better way, please let me know where I should be searching.
Any code snippets in explanations would be appreciated as I am still learning both Qt and C++.
Thanks!
-
Have a look at the QCoreApplication docs under Accessing Command Line Arguments. Generally the file name is passed to the program as the first argument by Windows for a dropped document. Registering a file extension and Open with... Action are matter of putting the correct thing into the correct place in the Windows registry. Most installer programs have easy to use support for doing this or you can use QSettings to do it from inside the program
-
Thanks ChrisW67.
I have attempted to implement the QCoreApplication::arguments() but I am still getting no commandline arguments passed.
My implementation:
@int main(int argc, char *argv[])
{
QApplication a(argc, argv);
arg1 = QCoreApplication::arguments().at(0);
if (QCoreApplication::arguments().size() > 1)
arg2 = QCoreApplication::arguments().at(1);
else
dummy();
ConfigRead w;
w.show();return a.exec();
}
void dummy ()
{
//nada so the else has something to do before ConfigRead (the window)
}@In the ConfigRead file where the UI is handled I added this code to my startup to see if I was getting anything in the arg1 and arg2 static QStrings defined in the header:
@
void ConfigRead::startup()
{
QString mpt = "EMPTY";
ui->targ1->setText(arg1);
if (arg1.isEmpty())
ui->targ1->setText(mpt);
ui->targ2->setText(arg2);
if (arg2.isEmpty())
ui->targ2->setText(mpt);initialize();
}@
It is always "Empty", even when I use cmd to call the program with various additional arguments:
@
C:\apppath>ApplicationName.exe "blarg"
C:\apppath>ApplicationName.exe -testing
C:\apppath>ApplicationName.exe C:\A.CFG
C:\apppath>ApplicationName.exe "C:\A.CFG"
@The QCoreApplication documentation says that the QCoreApplication might remove some arguments, but it doesn't say which ones or why it would remove them.
I am obviously missing something, but I am not sure where it is... Help?
-
Qt will remove some argument related to setting the Qt style or graphics renderer.
This simple code works for me for command line and dropped file:
@
#include <QtGui>
#include <QDebug>int main(int argc, char **argv)
{
QApplication app(argc, argv);QStringList args = QCoreApplication::arguments(); qDebug() << args; QWidget w; w.show(); return app.exec();
}
@(You may nee to add "CONFIG += console" to the pro file to get a console to see the output)
Since I cannot see where arg1 and arg2 are declared I cannot tell you if they are even the same variables in your code.
Edit: If the storage for arg1 and arg2 is declared in a header that is used in two different cpp files then you have two separate arg1/arg2 pairs... this why file2 cannot see the values in file1's variable arg1 and arg2.