PDA

View Full Version : Threading or signal/slot



lip
26th December 2010, 16:29
Dears,

I have a question about the QThread with signal/slot. I am wondering if using a signal to call a slot is also a kind of threading?
If not, is that true if I created a qthread(mythread) but calling it by a signal, they would stay in the same thread?

Actually, I am having a problem like, I have a mainwindow for the UI, when I calling some heavy function(e.g. rotation a very large picture in a pIII 500MHz), the main UI got a notice (Not Responding) if I click on it or just wait, I main UI got whitten=.=. I wanna to solve it so I tried to use signal/slot and put that heavy function into a thread, and I called that function(mythread.run()) when a receive a signal from the qpushbutton. Yet the problem still there!=.=
So I am wondering is that something I can do really like separating the main UI's operations(e.g. receiving signals for other thread functions) when I running a function on a queue(e.g. current running the rotation of a picture and will be followed by a flipping of the picture just rotated).

tbscope
26th December 2010, 17:22
Dears,

I have a question about the QThread with signal/slot. I am wondering if using a signal to call a slot is also a kind of threading?
No, it's not the same.


If not, is that true if I created a qthread(mythread) but calling it by a signal, they would stay in the same thread?
What is "they"?
You can use signals and slots to communicate between threads. But you need to take care that slots are executed in the thread you want.


Actually, I am having a problem like, I have a mainwindow for the UI, when I calling some heavy function(e.g. rotation a very large picture in a pIII 500MHz), the main UI got a notice (Not Responding) if I click on it or just wait, I main UI got whitten=.=. I wanna to solve it so I tried to use signal/slot and put that heavy function into a thread, and I called that function(mythread.run()) when a receive a signal from the qpushbutton. Yet the problem still there!=.=
So I am wondering is that something I can do really like separating the main UI's operations(e.g. receiving signals for other thread functions) when I running a function on a queue(e.g. current running the rotation of a picture and will be followed by a flipping of the picture just rotated).

Just put the rotating code in a thread, or better yet, use Qt Concurrent (there's an image manipulation example in the docs)

lip
26th December 2010, 19:31
No, it's not the same.


What is "they"?
You can use signals and slots to communicate between threads. But you need to take care that slots are executed in the thread you want.



Just put the rotating code in a thread, or better yet, use Qt Concurrent (there's an image manipulation example in the docs)

I see the point, but I am using qt 4.3 without qconcurrent class.

But for example i have following code, what i expected is, after the signal has been emitted, it will not wait util the slot function has been finished then goto the next statement. Because the slot function rotation2() is a function in a qthread class I inherited. Is there any condition I need to do to tell qt where to start the fork(threading) from master thread?

.....
connect::(someclass,SIGNAL(backrotation(double),my threadclass.SLOT(rotation2(double)));
.....
.....

int someclass::rotation(double &degree)
{
...
...
...
emit backrotation(degree); /*is there any method to start this function and no need to wait for it to finish for the return statement below*/
return 0;
}

Added after 32 minutes:

One more thing, if mythreadclass::run() does not include qtcpsocket, is it all fine?

tbscope
27th December 2010, 13:50
Something in the lines of this will work:


class RotationClass : public QObject
{
Q_OBJECT

public:
RotationClass();
void setImage(imagefile);

public slots:
void doRotation();

signals:
void rotationFinished(imagefile);
}

// Implement the RotationClass

// To use it:

RotationClass *rotator = new RotationClass;

rotator->setImage(someImage);
connect(anotherClass, SIGNAL(whatever()), rotator, SLOT(doRotation()));

QThread *workerThread = new QThread;
rotator->moveToThread(workerThread);
workerThread->start();


Make sure that your version of Qt starts an eventloop when doing workerThread->start().