PDA

View Full Version : how to import a file into qt when double clicking a file.



berlinud
16th September 2010, 15:06
I mean this:
If I have a program called A,and I set A as the default program for opening mp3 files in Linux,how can my program know it when the user opens a mp3 file in Linux desktop environment?

Lykurg
16th September 2010, 15:09
This has nothing to do with your application (Qt). It's a setting of your desktop environment and you have to set the setting there.

Zlatomir
16th September 2010, 15:27
@Lykurg: i think that what he meant to ask is: how the Linux OS is sending the file (name and path) to the associated qt application so that the programmer can open that particular file. (if that was the question - i don't know the answer :o )

Lykurg
16th September 2010, 15:31
@Lykurg: i think that what he meant to ask is: how the Linux OS is sending the file (name and path) to the associated qt application so that the programmer can open that particular file. (if that was the question - i don't know the answer :o )
Ok, if so, you have to parse the command line arguments in your main function and react on them. Normaly the file name is just append to the executable like: yourapp filename.mp3.

tbscope
16th September 2010, 15:32
Although suffixes will mostly do the job, a more correct way is to use mime types.

edit: ignore, I read the previous post wrong

berlinud
16th September 2010, 15:34
You are definitely right Zlatomir!
That is exactly what I mean.

berlinud
16th September 2010, 15:36
Would you mind explaining it a little more detail?
Since I am new in Linux programming.

Zlatomir
16th September 2010, 15:36
Ok, if so, you have to parse the command line arguments in your main function and react on them. Normaly the file name is just append to the executable like: yourapp filename.mp3.
I thought that might be the way, but i wasn't sure about that, thanks.

berlinud
16th September 2010, 15:43
what data will be transfered to me as the argument of main?
is that an absolute path?

Lykurg
16th September 2010, 15:52
is that an absolute path?I don't know, and you also can't know. Your app has to check all possibilities. Simple have a look on the value of argv (e.g. using qDebug()) and try to determain how you can handle the parameters.

Zlatomir
16th September 2010, 15:52
Here is a test project:


#include <QtGui>
#include <QStringList>

int main(int argc, char** argv){

QApplication a(argc, argv);
QTextEdit t;

QStringList l = qApp->arguments(); // qApp is a macro to QApplication instance and the arguments() returns a QStringList with the arguments

t.setText(l.join("\n"));
t.show();
return a.exec();
}


LE: I'm on win now and the first one is the full path and name of the application

berlinud
16th September 2010, 16:09
I tested it.
$ ./TestForMainArg ggg fff fff fff fff
the result is this:

./TestForMainArg
ggg
fff
fff
fff
fff

Zlatomir
16th September 2010, 16:11
create a text file rename it with .bla extension and create the association and see how the actual double click acts. (it should send the path too)

berlinud
16th September 2010, 16:12
When I turn the test program as the default program for mp3;
it shows that the arguments are transfered into the program as absolute path.

berlinud
16th September 2010, 16:13
yes you are right