UI is not responding in QThread
-
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_Hdisplay.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_Hcapture.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 9But UI is not responding
-
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_Hdisplay.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_Hcapture.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 9But UI is not responding
@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.