PDA

View Full Version : QDesktopServices or QProcess for launching external applications



Tepi
21st January 2010, 13:47
Hey,

I need to launch some external applications from my own program. So here's the simple question, which is preferred way to launch those applications QDesktopServices or QProcess?

I have done a small test with these and I would like to use QDesktopServices for this, but there seems to be some problem when trying to launching application executables.

Here's some code to clear things up...

Opening default web browser with some web page. This does the job as expected, good


QDesktopService::openUrl(QUrl("http://www.qtcentre.org"));

This is obviously because these kind of "resources" are marked to be opened with the web browser

Then there's also ability to open some files, for example self-executable JAR packages, with the same method


QDesktopService::openUrl(QUrl("file:////path/to/the/package.jar"));

This also goes as expected since Java Runtime is selected to be associated with this file type

Now, here comes the odd part. I would expect next code sample...


QDesktopService::openUrl(QUrl("file:////path/to/the/executable"));

... to start the application but it won't, instead it says in the console: "Error showing url: No application is registered as handling this file". This happens only in Linux, with Windows the executable is launched as expected... (of course with Windows the path in QUrl is different)
I understand that there's no application associated with these kind of files (executables), but shouldn't the system understand that the executable is just meant to be launched :confused:

Also the code...


QString program = "/path/to/the/executable";
QStringList arguments;

QProcess *process = new QProcess(this);
process->start(program, arguments);

is able to launch the application just fine.

So, do I have to use these both tehcniques to achieve what I want? Or is there a way to get the QDesktopServices to launch executables as expected, also in Linux? Maybe something to tweak from the Linux's settings? (I'm using Ubuntu, btw)

-Tepi

wysota
22nd January 2010, 11:17
openUrl() is for opening a data file using a default handler for this file in the file system. So passing it an executable would try to find a program to open this executable with (like a hex editor or something). This is not what you want. Use QProcess.

Tepi
25th January 2010, 07:40
Thanks for your reply, wysota.

I'll use QProcess then. Although it would seem quite logical to be able to launch also executables with QDesktopServices, but that's only my opinion ;) I understand that QDesktopServices is designed for the purpose of opening differend kind of file types with their default handlers.

wysota
25th January 2010, 09:28
I'll use QProcess then. Although it would seem quite logical to be able to launch also executables with QDesktopServices, but that's only my opinion ;)

Why? This class is for opening a datafile with a program. In my system I can have a default file handler for .exe files to be set to open them in a hex editor or something. What should QDesktopServices do if I passed it such an exe file? Open it in the hex editor or run the executable?

Tepi
25th January 2010, 11:02
My opinion is that if you have set some special file handler for the .exe files (which I think is quite rare occasion) then it should be opened with that handler (for example that hex editor), otherwise it should just be executed (as there's no file handlers assigned for the type). And again this is just my humble opinion and the first impression which I had when I tried the QDesktopServices ;)

Btw, on Windows it launches the .exe file automatically if you haven't changed the handler for the .exe files. So this is why i was assuming the same functionality for the Linux systems... I don't know if this is a distribution dependent issue, but at least with Ubuntu it won't launch it.

And thanks again for the comments Wysota.

wysota
25th January 2010, 16:17
My opinion is that if you have set some special file handler for the .exe files (which I think is quite rare occasion) then it should be opened with that handler (for example that hex editor), otherwise it should just be executed (as there's no file handlers assigned for the type). And again this is just my humble opinion and the first impression which I had when I tried the QDesktopServices ;)
And if we were talking about an image type? Should the image be executed too if there was no handler assigned to it? What purpose would it serve?


Btw, on Windows it launches the .exe file automatically if you haven't changed the handler for the .exe files.
"It" meaning what? QDesktopServices? Maybe there's a handler installed for executable files on Windows. There is no such handler on Unix systems, it wouldn't make much sense. It is the executable bit that marks if the file can be executed or not and not the file type.

Tepi
26th January 2010, 08:56
And if we were talking about an image type? Should the image be executed too if there was no handler assigned to it? What purpose would it serve?


Nope, of course not. That's not the point. In the case some image type doesn't have handler assigned, system would ask user with what program the file should be opened, as it is doing now by default. Why would the image file type have "execute" permissions? I see no point on that...

So when we have some executable file with "execute" permissions which has no hadler assigned to it, wouldn't it seem logical just to launch it?



"It" meaning what? QDesktopServices?


Yep, "it" meaning QDesktopServices.

I looked the "File types" list on Windows and there wasn't anything assigned to the .exe files. So to test it I assigned a Hex Editor for handling the .exe files. It did the job as expected, opened .exe files with Hex Editor. But before I added the handler I noticed that there wasn't any special handler assigned to the .exe files but it was marked as "Type of File: Application" and I quess this is the reason why the Windows understands that the file is meant to be executed.

So here I come back to the argument that if in unix system we have a file that hasn't got any handler assigned to it and it still has "execute" permissions allowed, why shouldn't it be launched by the QDesktopServices? Same manner as it is launched when you double click the icon on your file system.

And these replies aren't meant for offence by any means, on the contrary. Just wanted to know about this issue which bothered me...

-Tepi