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. UI is not responding in QThread
Qt 6.11 is out! See what's new in the release blog

UI is not responding in QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 239 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.
  • D Offline
    D Offline
    DhivyaJR
    wrote on last edited by
    #1

    Hi I am trying a simple program where I created a sub-class for QObject and emitting a signal from it and trying to capture in a slot and display the value on UI , it enters the slot, but and printing the qdebug () message, but UI is not responding

    Here is my code snnippet.
    display.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QString>
    #include "mythread.h"
    
    namespace Ui {
    class dialog;
    }
    
    class dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit dialog(QWidget *parent = 0);
        ~dialog();
        bool stop;
    private:
        Ui::dialog *ui;
    
    public slots:
        void onValueChanged(int);
    
    private slots:
        // for Start button
        void on_pushButton_clicked();
    
        // for Stop button
        void on_pushButton_2_clicked();
    };
    
    #endif // DIALOG_H
    
    

    display.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include "mythread.h"
    #include "process.h"
    #include "capture.h"
    #include <QDebug>
    
    dialog::dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::dialog)
    {
        ui->setupUi(this);
    }
    
    dialog::~dialog()
    {
        delete ui;
    }
    
    // Absorb the signal emitted from a run() method
    // and reflect the count change to the count label
    // in our dialog
    
    void dialog::onValueChanged(int count)
    {
        qDebug()<<"On Value changed"<< count;
        ui->label->setText(QString::number(count));
    }
    
    // Start button
    void dialog::on_pushButton_clicked()
    {  
        Capture capture_thread;
        QObject::connect(&capture_thread,&Capture::inputAvailable,
                        this, &dialog::onValueChanged);
        capture_thread.startCapture();
    }
    
    // Stop button
    void dialog::on_pushButton_2_clicked()
    {
        stop = true;
    }
    
    

    capture.h

    #ifndef CAPTURE_H
    #define CAPTURE_H
    
    #include <QObject>
    
    namespace Ui {
    class Capture;
    }
    class Capture : public QObject
    {
        Q_OBJECT
    public:
        explicit Capture(QObject *parent = nullptr);
    signals:
         void inputAvailable(int i);
    
    public slots:
        void startCapture();
    
    };
    
    #endif // CAPTURE_H
    
    

    capture.cpp

    #include "capture.h"
    #include <QMutex>
    #include <QThread>
    #include "dialog.h"
    static int check = 0;
    Capture::Capture(QObject *parent) : QObject(parent)
    {
    
    }
    
    void Capture::startCapture()
    {
        dialog d;
        while(1)
        {
            int i = check;
            QMutex mutex;
            // prevent other threads from changing the "Stop" value
            mutex.lock();
            if (d.stop == true)break;
            mutex.unlock();
    
            // emit the signal for the count label
            emit inputAvailable(i);
    
            check++;
            // slowdown the count change, msec
            QThread::msleep(500);
        }
    
    }
    
    

    When Start button is clicked from UI,I am getting output as follows

    10:20:56: Starting /home/dhivyajr/build-qThreadDisplay_learining-Desktop_Qt_5_15_2_GCC_64bit-Debug/qThreadDisplay_learining ...
    On Value changed 0
    On Value changed 1
    On Value changed 2
    On Value changed 3
    On Value changed 4
    On Value changed 5
    On Value changed 6
    On Value changed 7
    On Value changed 8
    On Value changed 9
    

    But UI is not responding

    jsulmJ 1 Reply Last reply
    0
    • D DhivyaJR

      Hi I am trying a simple program where I created a sub-class for QObject and emitting a signal from it and trying to capture in a slot and display the value on UI , it enters the slot, but and printing the qdebug () message, but UI is not responding

      Here is my code snnippet.
      display.h

      #ifndef DIALOG_H
      #define DIALOG_H
      
      #include <QDialog>
      #include <QString>
      #include "mythread.h"
      
      namespace Ui {
      class dialog;
      }
      
      class dialog : public QDialog
      {
          Q_OBJECT
      
      public:
          explicit dialog(QWidget *parent = 0);
          ~dialog();
          bool stop;
      private:
          Ui::dialog *ui;
      
      public slots:
          void onValueChanged(int);
      
      private slots:
          // for Start button
          void on_pushButton_clicked();
      
          // for Stop button
          void on_pushButton_2_clicked();
      };
      
      #endif // DIALOG_H
      
      

      display.cpp

      #include "dialog.h"
      #include "ui_dialog.h"
      #include "mythread.h"
      #include "process.h"
      #include "capture.h"
      #include <QDebug>
      
      dialog::dialog(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::dialog)
      {
          ui->setupUi(this);
      }
      
      dialog::~dialog()
      {
          delete ui;
      }
      
      // Absorb the signal emitted from a run() method
      // and reflect the count change to the count label
      // in our dialog
      
      void dialog::onValueChanged(int count)
      {
          qDebug()<<"On Value changed"<< count;
          ui->label->setText(QString::number(count));
      }
      
      // Start button
      void dialog::on_pushButton_clicked()
      {  
          Capture capture_thread;
          QObject::connect(&capture_thread,&Capture::inputAvailable,
                          this, &dialog::onValueChanged);
          capture_thread.startCapture();
      }
      
      // Stop button
      void dialog::on_pushButton_2_clicked()
      {
          stop = true;
      }
      
      

      capture.h

      #ifndef CAPTURE_H
      #define CAPTURE_H
      
      #include <QObject>
      
      namespace Ui {
      class Capture;
      }
      class Capture : public QObject
      {
          Q_OBJECT
      public:
          explicit Capture(QObject *parent = nullptr);
      signals:
           void inputAvailable(int i);
      
      public slots:
          void startCapture();
      
      };
      
      #endif // CAPTURE_H
      
      

      capture.cpp

      #include "capture.h"
      #include <QMutex>
      #include <QThread>
      #include "dialog.h"
      static int check = 0;
      Capture::Capture(QObject *parent) : QObject(parent)
      {
      
      }
      
      void Capture::startCapture()
      {
          dialog d;
          while(1)
          {
              int i = check;
              QMutex mutex;
              // prevent other threads from changing the "Stop" value
              mutex.lock();
              if (d.stop == true)break;
              mutex.unlock();
      
              // emit the signal for the count label
              emit inputAvailable(i);
      
              check++;
              // slowdown the count change, msec
              QThread::msleep(500);
          }
      
      }
      
      

      When Start button is clicked from UI,I am getting output as follows

      10:20:56: Starting /home/dhivyajr/build-qThreadDisplay_learining-Desktop_Qt_5_15_2_GCC_64bit-Debug/qThreadDisplay_learining ...
      On Value changed 0
      On Value changed 1
      On Value changed 2
      On Value changed 3
      On Value changed 4
      On Value changed 5
      On Value changed 6
      On Value changed 7
      On Value changed 8
      On Value changed 9
      

      But UI is not responding

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @DhivyaJR said in UI is not responding in QThread:

      void dialog::on_pushButton_clicked()
      {
      Capture capture_thread;
      QObject::connect(&capture_thread,&Capture::inputAvailable,
      this, &dialog::onValueChanged);
      capture_thread.startCapture();
      }

      startCapture() is a blocking call, so you are blocking the UI thread.
      Also, I don't see where you are using any other threads? Everything is running in UI thread.
      Please read https://doc.qt.io/qt-5/thread-basics.html if you want to do multi threading.

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

      1 Reply Last reply
      5

      • Login

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