avh
30th May 2008, 18:12
So, I'm a situation that's similar to the Mandelbrot example that ships with Qt, with a few wrinkles. I'm writing a GUI for a scientific computing application, and it needs to stay responsive while the application is doing something computationally expensive.
Unlike the Mandelbrot example, where the computationally expensive code appears directly in the worker thread, it has already been implemented and lives in a global object, whose member functions I call in order to interface with that code. I have a QThread subclass, appearing below, that I want to use as a worker thread, and it has some slots that call the global object's troublesome member functions.
Everything compiles and runs, but, when something happens that triggers one of the QThread's slots, the GUI still goes nonresponsive.
I think this might be because the GUI also relies on member functions of that global object for various things, or maybe I just have an oversimplified conception of how to use a QThread.
Regardless, my question is this: how does using a QThread to keep a GUI responsive change when one is calling member functions of a global object like I am attempting to do?
class ReconstructionThread : public QThread {
Q_OBJECT
public:
ReconstructionThread(QObject* parent) : QThread(parent) {}
void run();
public slots:
void startMethod(int iToStart);
void stopMethod(int iToStop);
signals:
void methodFinished();
};
#include "ReconstructionThread.h"
extern ImagingDetectionInterconnect interconnect;
void ReconstructionThread::run() {
exec();
}
void ReconstructionThread::startMethod(int iToStart) {
interconnect.start(iToStart);//This is the troublemaker, here: it can take an hour
//or two to return
emit methodFinished();
}
void ReconstructionThread::stopMethod(int iToStop) {
interconnect.stop(iToStop);
}
Unlike the Mandelbrot example, where the computationally expensive code appears directly in the worker thread, it has already been implemented and lives in a global object, whose member functions I call in order to interface with that code. I have a QThread subclass, appearing below, that I want to use as a worker thread, and it has some slots that call the global object's troublesome member functions.
Everything compiles and runs, but, when something happens that triggers one of the QThread's slots, the GUI still goes nonresponsive.
I think this might be because the GUI also relies on member functions of that global object for various things, or maybe I just have an oversimplified conception of how to use a QThread.
Regardless, my question is this: how does using a QThread to keep a GUI responsive change when one is calling member functions of a global object like I am attempting to do?
class ReconstructionThread : public QThread {
Q_OBJECT
public:
ReconstructionThread(QObject* parent) : QThread(parent) {}
void run();
public slots:
void startMethod(int iToStart);
void stopMethod(int iToStop);
signals:
void methodFinished();
};
#include "ReconstructionThread.h"
extern ImagingDetectionInterconnect interconnect;
void ReconstructionThread::run() {
exec();
}
void ReconstructionThread::startMethod(int iToStart) {
interconnect.start(iToStart);//This is the troublemaker, here: it can take an hour
//or two to return
emit methodFinished();
}
void ReconstructionThread::stopMethod(int iToStop) {
interconnect.stop(iToStop);
}