PDA

View Full Version : Just launch a function and do not append to it?



hakermania
6th July 2011, 18:41
I am not sure you got me....
What I need is to just launch a function like it was a separate program.
For example, if a timer was connected to a slot like this:


void slot_1(){
while(1){
sleep(10);
}
}

the program would stack! I don't want it to 'stack'. I want to go forward even if such a function was called. Is this possible?
To be more realistic, I have a system command that reaches some files from the internet, and it may be late sometime and make my program unresponsive to clicks etc because of this....

PS: I though that QTimer::SingleShot was for that job but it does append to the slot unfortunately, so, after all, what's the difference between

QTimer::singleShot(0, this, SLOT(function()));
and

function();

Thanks for any answers!

stampede
6th July 2011, 18:59
I think what you need is to run the function in separate thread. Search for examples on Qt Centre wiki or threads on forum.

hakermania
8th July 2011, 00:22
Hm, thank google wasn't very helpful....
Should I use QThread or QObject? This wasn't clear too...
Do I have to create my own classes? Can I avoid this? I need something simple, like:

QObject object;
object.setFunction(MainWindow::MyFunction_that_I_w ant_to_be_launched());
object.run
//more code here that will be executed without waiting the MyFunction_that_I_want_to_be_launched() to finish running

I don't know what are they talking about producers and consumers....:confused:

stampede
8th July 2011, 00:35
If you want to run only one function, take a look at QtConcurrent module and QtConcurrent::run (http://doc.qt.nokia.com/4.7-snapshot/qtconcurrentrun.html) method.

I don't know what are they talking about producers and consumers....
Because it's a standard example used to explain multithreading concepts.

hakermania
8th July 2011, 16:02
Yeah! This did the trick man! Thanks :)