Enhebrado en Qt

I read on internet that subclassing (that deriving a class from Qthread) and then over writing run function to perform the required task is not the correct way to use Qthread. But i have read in some books and on Qt documentation to use the subclassing way?

can you tell me why shouldn't use the subclassing way? Subclassing Qthread is an easy way to use thread (for me because i am a beginner).


preguntado el 10 de marzo de 12 a las 10:03

@Nawaz, this may come from the recent answer to one of my questions: stackoverflow.com/questions/9638226/… -

3 Respuestas

See this great article about QThread: The great QThread Mess by Christoph Eckert, which lead me to use QThread's this way, now: Threading without the headache by Bradley T. Hughes

Bottom line: IMHO, the easiest way to use QThread is to create an QObject subclass, use signals/slots and use moveToThread to have the object live in a different thread with a different event loop.
Unfortunately, this doesn't work if you absolutely need a "while(true)" kind of thread, but it is very often avoidable in Qt.

respondido 10 mar '12, 11:03

will you please mind to tell what does this means! it is preety confusing for a beginner. "all of the functions in QThread were written and intended to be called from the CREATING THREAD, not the thread that QThread starts" - Umer Farooq

The creating thread is the thread that "owns" the QThread object. QThread itself is not the thread, it manages the thread in which resides a different event loop, which explains "the thread that QThread starts" - chris browet

QThread was designed and is intended to be used as an interface or a control point to an operating system thread, not as a place to put code that you want to run in a thread. We object-oriented programmers subclass because we want to extend or specialize the base class functionality. The only valid reasons I can think of for subclassing QThread is to add functionality that QThread doesn’t have, e.g. perhaps providing a pointer to memory to use as the thread’s stack, or possibly adding real-time interfaces/support. Code to download a file, or to query a database, or to do any other kind of processing should not be added to a subclass of QThread; it should be encapsulated in an object of it’s own.

for more details please check this..


respondido 10 mar '12, 10:03

@ shobi Sir what does creating thread means? and What does this statement means? Never mind:) "all of the functions in QThread were written and intended to be called from the creating thread, not the thread that QThread starts" - Umer Farooq

@UmerFarooq if u want to create a thread then u can do it by just creating the object of QThread class, u dont need to have a class that is subclassing the QThread class. - zapato

yeah thanks sir there is a bit confusion left. as Koying mentioned:" it manages the thread in which resides a different event loop". What does this means - Umer Farooq

it means that ur QThread is like a wrapper which handles threading. Now when u instantiate an object of QThread at that time the new thread is created, this means that the method in which QThread's object is created is on main thread and the method in which u call the QThread::start() is on the newly created thread. - zapato

Qt is very old, this article is quite new. The interface of QThread clearly intends on deriving. Some functionality, like not having a message loop, can , solamente be achieved by deriving from QThread. - edA-qa mort-ora-y

El Documentación de QThread explicitly states that subclassing is the right thing to do:

Para crear sus propios hilos, subclase QThread y reimplementación run().

This is somewhat puzzling though, because (as shobi points out in otra respuesta) it is an ugly design and there is an alternative solution.

contestado el 23 de mayo de 17 a las 13:05

shobi's answer neglects to mention the API is currently not fully suitable for the other solution. Unlike a nice API like boost thread, you can't just easily provide any function to act in the thread -- the signal/slot approach is far more complicated and assumes you want an event loop, which may not be true. - edA-qa mort-ora-y

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.