PDA

View Full Version : Make ui stay responsive when executin thread



aguleo
26th January 2013, 17:44
I subclassed QThread and it's run method like this:

mythread.h


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QString>

class myThread : public QThread
{
Q_OBJECT
public:
myThread();
void run();

private:
//...
};

#endif // MYTHREAD_H


mythread.cpp


#include "mythread.h"
#include <iostream>

myThread::myThread()
{
//...
}

void myThread::run()
{
for(int i=0; i<10; i++)
{
QThread::sleep(1);
std::cout << i << std::endl;
}
}


How and when can i call this thread in order que keep the application responsive?

Sorry if it makes no sense ... my first thread experience :)

Santosh Reddy
26th January 2013, 18:17
How and when can i call this thread in order que keep the application responsive?
It doesn't matter, whenever you want...:)

aguleo
26th January 2013, 19:18
How is that?
i'm calling it from QMainWindow and i have to wait 10 seconds before i hava access to the menu again!

wysota
26th January 2013, 19:20
i'm calling it
"It", being what exactly?

aguleo
26th January 2013, 19:33
I attach "it" :)
I guess i can not call it from that member ... isn't it?

Santosh Reddy
26th January 2013, 19:45
How is that?
i'm calling it from QMainWindow and i have to wait 10 seconds before i hava access to the menu again!
No it won't wait, (That is whats Thread's do)

To run the thread you should be calling QThread::start() (not QThread::run()), this will run the thread in it own (new thread) context

If you call QThread::run(), it will execute in the calling thread context.

aguleo
26th January 2013, 19:57
Ok ... made some progress.
I was creating the thread inside a member function ... this way it ended unexpectedly ...
But now i have a question. What happens when i call the same option again while it is still running?
I ask because apparently nothing does!

Santosh Reddy
26th January 2013, 20:30
But now i have a question. What happens when i call the same option again while it is still running?
If the thread is running, then nothing will happen.
If the thread is not running, the thread will start running, provided all waiting conditions are cleared.

lightydo
26th January 2013, 20:53
if you want to run another thread instance, then create an array of threads and run them as you require. don't forget to capture their ending event so you know which are running.
if you need to synchronize concurrent access to shared item, conside QSharedMemory or QSempahore.

wysota
26th January 2013, 23:53
Maybe you should first tell us what you want the thread to do?

aguleo
27th January 2013, 11:35
There was nothing specific on my mind. I read something about saving large files to disk in the "C++ GUI programming with Qt4" and got curiouse.

Maybe you should first tell us what you want the thread to do?
You mean that the approach will depend on what i want to do ... is it so?

wysota
27th January 2013, 12:47
Yes, for many usecases using a thread is either not possible or simply bad idea.

Here, read this: Keeping the GUI Responsive