QThread with signals and slots
-
Reference the blog and "this":http://developer.qt.nokia.com/wiki/ThreadsEventsQObjects wiki entry. Only based on the official Qt documentation, it is way too easy to get it wrong. Also, do not, please, use moveToThread(this) inside your QThread subclass. You probably don't understand what that does exactly, and what the consequences are.
-
Thank you very much. You are right. I am a bit confused now about movetothread while I know it is used later in the thread for events and ...
I need to get help by reading a source code which has correct implementation.
As I remember in the documents , ::Start() should not be called from the thread itself !!! while I see in the code "queuedcustomtype" (attached in the Qt Sources) the start has been called from a method inside the Thread Subclass !!!!! ? -
[quote]I need to get help by reading a source code which has correct implementation. As I remember in the documents , ::Start() should not be called from the thread itself ![/quote]
What do you mean with that? It's supposed to be called from the thread your thread object is living in. Calling a custom method foo() which in turns calls start() is perfectly fine.
-
Copy/paste from the mentioned wiki entry:
@
class Worker : public QObject
{
Q_OBJECTpublic slots:
void doWork() {
/* ... */
}
};/* ... */
QThread *thread = new QThread;
Worker *worker = new Worker;
connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));
worker->moveToThread(thread);
thread->start();
@ -
[quote author="Andre" date="1306745564"]Copy/paste from the mentioned wiki entry:
@
class Worker : public QObject
{
Q_OBJECTpublic slots:
void doWork() {
/* ... */
}
};/* ... */
QThread *thread = new QThread;
Worker *worker = new Worker;
connect(obj, SIGNAL(workReady()), worker, SLOT(doWork()));
worker->moveToThread(thread);
thread->start();
@
[/quote]Thank you very much. so , it means SubClassing is already over since Qt 4.4(which it is not a Abstract class anymore) . so , I should use thread always this way !?
by the way , the Qt wiki looks excelent .@peppe : in that example from Qt Sources I see that the thread::Start has been called from thread subclass and not the thread that object lives in.
-
It is not impossible to subclass QThread and use it that way, but it is no longer the recommended way to do things. I won't say you should always use the create-a-worker-QObject-and-move-it-to-a-vanilla-QThread method, but in general, yes, that is the way to go. Especially if you plan to start using signals and slots.
-
[quote author="Satmosc" date="1306746483"]
@peppe : in that example from Qt Sources I see that the thread::Start has been called from thread subclass and not the thread that object lives in.
[/quote]What's the example you're talking about? And "from the thread subclass" doesn't mean much; as I said, adding a method which in turn calls start() and calling that method is fine (and I doubt start() is getting called from inside run()).
-
peppe: Check examples from QtSources Threading/queuedcustomtype
and in file "renderthread.cpp", Line 66:@//![processing the image (start)]
void RenderThread::processImage(const QImage &image)
{
if (image.isNull())
return;m_image = image; m_abort = false; start();
}@