Multiple object in QThread

  • Can put multiple of object or duplicate object/Qwidget into Qthread?

    for example

    class test_class : public Qwidget
    do something
    void main(){
    Qthread *test_thread = new Qthread;
    for ( int n=0 ; n<5; n++{
     testobj[n] = new test_class;

  • Lifetime Qt Champion


    Yes you can have multiple QObject moved to a QThread. Note that you can't call GUI related methods from a thread that is not the main thread.

  • Can I move QWidget to a thread. Basically the QWidget in my class only set some value for Qpixmap but doesn't plot them. It plotted in different functions

  • @s002wjh said in Multiple object in QThread:

    Can I move QWidget to a thread



    As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.

  • Qt Champions 2017

    If you can inherit from QObject instead , then it would be better.

  • @s002wjh The QWidget and QPixmap are GUI classes so it's not permitted to modify them in another thread than the main thread.

    If you want to change some image pixels you can use QImage that is not a GUI class.

  • K tux.

    How do I pass dada array between main thread and worker thread. Using connect or can I set pointer. PointerWorkerobj->function(&Data) with mutex ?

  • @s002wjh It depens. You said that your worker thread will modify some data, so I recommend you to to protect the concurrent access with a Mutex and use a pointer to access the data. This will give you a better performance instead of making a copy of the data.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.