Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QThread within the same class

    General and Desktop
    qthread qlabel
    3
    5
    1546
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      marlenet15 last edited by

      I have a QPushButton and two QLabel's. When the pushbutton is clicked, each QLabel will display over 1000 frames every 500 milliseconds or so making it appear as a video. However, each QLabel will display different frames from different directorie's locations but both QLabels need to display their frames/video at the same time. After some research, I believe I need two threads in order to run them at the same time. I have written the important sections of the code below.
      I am not sure how to use Threads here. Will each tread call the same timer or is it two separate ones? I know for sure that both threads need to call the updateLabel but I am not sure how to make this work and where to put the threads.

      #include "setup.h"
      #include "ui_setup.h"
      #include <QFile>
      #include <QTextStream>
      #include <QDir>
      #include <QTimer>
      #include <QThread>
      #include <QDebug>
      
      setup::setup(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::setup)
      {
          ui->setupUi(this);
          timer = new QTimer(this);
          connect(timer, SIGNAL(timeout()), this, SLOT(updateLabel())); //not sure how this will work
          directory1 = QString("directory1_name");
          directory2= QString("directory2_name");
      }
      
      void setup::updateLabel(QString directory)
      {
        // if fileList is empty
         //  timer->stop
      
      else {
          //save images into vector
      }
      ui->label1->setPixmap(images[x]);
      ui->label1->update();
      }
      
      void setup::onClickPlay()
      {
          QDir myDir(directory1);
          filelist = myDir.entryList(QStringList("*.png")); //saves all images
          timer->start(500);
      }
      
      setup::~setup()
      {
          delete ui;
      }
      
      1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion last edited by

        In the code you provided I cannot see any threads.
        In general there is no need for threads in this case: just use a timer and update the labels on each timer event.

        A note: in Qt you should NEVER modify UI from another threads! This is not supported.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply Reply Quote 2
        • M
          marlenet15 @jsulm last edited by

          @jsulm I didn't add any threads since I wasn't sure how or where to add them. But how do I updated both QLabels at the same time id threads are not necessary?

          kshegunov 1 Reply Last reply Reply Quote 0
          • kshegunov
            kshegunov Moderators @marlenet15 last edited by kshegunov

            @marlenet15
            Strictly speaking you can't unless you call QWidget::repaint() on each of the widgets manually from the same function/slot. Otherwise Qt will schedule the paint events as it sees fit, which is absolutely fine for most of the cases. Also you can't call QWidget functions from a separate thread (as @jsulm noted), it must be from the main thread. So @jsulm is absolutely correct that you don't actually need any threads and stand to gain nothing from them.

            Kind regards.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply Reply Quote 0
            • M
              marlenet15 last edited by

              I guess you are right. I will work on this without the Threads. Thank you for your help!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post