Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread within the same class

QThread within the same class

Scheduled Pinned Locked Moved Solved General and Desktop
qthreadqlabel
5 Posts 3 Posters 2.0k Views
  • 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 Offline
    M Offline
    marlenet15
    wrote on 16 Feb 2016, 23:58 last edited by
    #1

    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
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Feb 2016, 05:50 last edited by
      #2

      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 17 Feb 2016, 16:02
      2
      • J jsulm
        17 Feb 2016, 05:50

        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.

        M Offline
        M Offline
        marlenet15
        wrote on 17 Feb 2016, 16:02 last edited by
        #3

        @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?

        K 1 Reply Last reply 17 Feb 2016, 16:27
        0
        • M marlenet15
          17 Feb 2016, 16:02

          @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?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 17 Feb 2016, 16:27 last edited by kshegunov
          #4

          @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
          0
          • M Offline
            M Offline
            marlenet15
            wrote on 17 Feb 2016, 18:04 last edited by
            #5

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

            1 Reply Last reply
            0

            5/5

            17 Feb 2016, 18:04

            • Login

            • Login or register to search.
            5 out of 5
            • First post
              5/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved