PDA

View Full Version : QProcess object not emitting finished(int) signal



Tiansen
14th November 2008, 08:42
Hello,

I have a weird trouble. I have created a QProcess object and started an external program. But... when program finishes, object is not emitting finished signal. How is this possible? What can I do to workaround this?

My code example:

launcher::launcher(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
QProcess program;
Sleep(3000);
program.start("/path/to/my/program.exe");
connect(&program, SIGNAL(finished(int)), this, SLOT(OnExit()));
}


void launcher::OnExit()
{
QMessageBox::information(this,"","Izhod");
}

wysota
14th November 2008, 09:03
Your program doesn't even start. The QProcess object that controls it gets destroyed when you leave the constructor. Create it on heap instead.

spirit
14th November 2008, 09:07
this slot works, but your programm terminated immediately. create QProcess in a heap.
wysota, you was first :D

Tiansen
14th November 2008, 09:26
Well, actually my program does not end. I did not paste the whole constructor code :).
After connect.... line I have this:

while (true)
{
Sleep(1000)
}

So I stay in constructor and my program keeps running. It stays in constructor because I don't want it to open GUI. It's ugly I know but it works and runs hidden. With Sleep I prevent 100% CPU utilization.

spirit
14th November 2008, 09:28
create console app and run process.

Tiansen
14th November 2008, 09:32
I don't want to open any window, not even console. I want it to run as services do (completely hidden), but don't know how to program real service.

Now it seems like signal-slots communication does not work until execution leaves constructor??

spirit
14th November 2008, 09:35
if you need a service you can use QtService from Qt Solutions.

Tiansen
14th November 2008, 09:46
I never programmed a service and I want to be sure that I cannot achieve my goal any other way before diving into this.

I still do not understand why it does not work as it is now.

Tiansen
14th November 2008, 09:50
If I comment out while loop so that it opens GUI then object indeed emits signal. So it seems that with that loop I blocked processing of signals. Any other idea to create a "hidden" launcher without programming a service?

Tiansen
14th November 2008, 10:00
Hmmm, now I figured out something. I can leave constructor as in first post (without while loop), but just comment out following line in main.cpp:

//w.show();

and it works as intended (launches program, shows dialog box when program exits and stays completely hidden). Does this have any side effect?

spirit
14th November 2008, 10:06
what do you want to achive? do you use any GUI in your app or you need only start another process, so you don't need GUI?

Tiansen
14th November 2008, 10:13
Okay, I wil try to explain:

1. I DON'T want ANY window (NO GUI, no console either)

2. I just want to first run 2 other processes and then restart them if one of them dies/exits.

This is all I want. And it seems I can achieve that if i comment out w.show().


P.S.: I only had that QMessageBox because I tested behaviour. Application must stay completely silent normally.

spirit
14th November 2008, 10:38
there is some "magic" to hidding your app (i.e. that it don't appear in task bar), but for this "magic" you need to use platform dependent code.
so, I suggest to use QtService or take a look at QSystemTrayIcon

Tiansen
14th November 2008, 13:17
I think I "invented" such magic too :D