PDA

View Full Version : use Qtimer in gui application with some thread at background



k.qasempour
11th July 2012, 10:06
how can i use Qtimer in gui application with some thread at background?
plz give me an example

codeman
11th July 2012, 10:21
What would you like to achieve in detail?

high_flyer
11th July 2012, 11:43
how can i use Qtimer in gui application with some thread at background?
This is generally bad idea, specially when its asked by a newbie.
You probably don't need a thread.
How to use a QTimer.

k.qasempour
11th July 2012, 12:47
well... let me explain the situsatuion... for example i have 100 links. im trying to download each of them with an Qhttp object..

for(link1 to link100)
{
.
.
.
QHttp myHttp = new QHttp(some host);
myHttp.get(some path)
}

till here.. ok?

as u know Qhttp inhertis from Qobject and also Qobject inherits from Qthread so, each instance of Qhttp is a thread by itself... thus the multithreading part is done... is it right?

now i need to make some delay between my requests.. for example 5 seconde.... i add this code to the above code....


for(link1 to link100)
{
.
.
.
QHttp myHttp = new QHttp(some host);
myHttp.get(some path)
QTimer timer;
connect(timer,SIGNAL(timeout()),this,SLOT(someslot ()))
timer.start(5000);
}

but it does not work....

is it clear? if not i can paste my code here.. thanks

wysota
11th July 2012, 12:48
QObject does not inherit QThread. QHttp is not a thread.

k.qasempour
11th July 2012, 12:51
so u r right... i made a mistake ... QThread inherits Qobject...
am i wrong? i mean each instance of QHttp isnt a thread by it self?

Lesiok
11th July 2012, 12:56
k.qasempour, the question of the foundations of C++ : what happens with timer at line 11 ?

for(link1 to link100)
{
.
.
.
QHttp myHttp = new QHttp(some host);
myHttp.get(some path)
QTimer timer;
connect(timer,SIGNAL(timeout()),this,SLOT(someslot ()))
timer.start(5000);
}

k.qasempour
11th July 2012, 13:00
i know what u mean... but now my question is
each instance of QHttp isnt a thread by it self?

high_flyer
11th July 2012, 13:11
each instance of QHttp isnt a thread by it self?
You tell us!
QHttp

wysota
11th July 2012, 13:18
i mean each instance of QHttp isnt a thread by it self?
Read the docs, please.

saman_artorious
11th July 2012, 20:42
As far as it is concerned with this particular post, I solve my own problem using single shots. let's say you need to execute a slot two seconds after:

QTimer::singleshot(2000, this, SLOT(slotname()));

I used singleshot inside most of my objects in a project. if you still have problems with it, I can check it out for further details.

and, singleshot or your QTimer.start do not delay the execution of the instruction below them. each timers as soon as called, will be run as a seperate thread. it's like a parent process creating a child of his own using fork() in c. ;)

wysota
11th July 2012, 21:47
each timers as soon as called, will be run as a seperate thread.
No, that's false.

k.qasempour
12th July 2012, 05:28
it does not work again.... :(
here is my code... whats the problem?

QTimer::singleShot(2000, this, SLOT(fake()));


void MainWindow::fake()
{
int a;
a++;

}

wysota
12th July 2012, 08:40
This code does not do anything so how do you know it doesn't work?