Search code examples
c++multithreadingqtqt5qthread

GUI lag issue when using thread


This is the code i'm using now the issue is when i press on the pushbutton the thread is starting and the value in line edit is updating. but it slows down the GUI overall.

I am learning QThread so implemented this code and facing difficulties in it.

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
 {
    ui->setupUi(this);
 } 

 MainWindow::~MainWindow()
 {
   delete ui;
 }

 void MainWindow::on_pushButton_clicked()
 {
  qDebug()<<"pd1";
  work->moveToThread(thread);

  connect(work,    SIGNAL(finished()), work,   SLOT(deleteLater()));
  connect(thread,  SIGNAL(started()),  work,   SLOT(process()));
  connect(work,    SIGNAL(datar(int)), this,   SLOT(display(int)));

  connect(work,    SIGNAL(finished()), thread, SLOT(quit()));
  connect(thread,  SIGNAL(finished()), thread, SLOT(deleteLater()));
  thread->start();
  qDebug()<<"pd2";
} 

 void MainWindow::display(int i)
{
  ui->lineEdit->setText(QString::number(i));
}

void MainWindow::on_pushButton_2_clicked()
{
   qDebug()<<"In push button - 2";
   for(int i = 0; i < 200; i++)
   {
      qDebug()<<i;
      ui->lineEdit_2->setText(QString::number(i));
   }
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <worker.h>
#include <QThread>


namespace Ui {
 class MainWindow;

}

class MainWindow : public QMainWindow
{
  Q_OBJECT

 public:
   explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();

 public slots:
 void display(int i);

 private slots:
 void on_pushButton_clicked();

 void on_pushButton_2_clicked();

 void on_pushButton_3_clicked();

 void on_pushButton_4_clicked();

 private:
 Ui::MainWindow *ui;
 worker* work = new worker();
 QThread* thread = new QThread;

 };

worker.cpp

 #include "worker.h"
 #include <QDebug>

 worker::worker(QObject *parent) : QObject(parent)
 {

 }

 void worker::process()
{
   int index = 0;
   qDebug()<<"In here";
   while(true)
     {
        qDebug("Hello World!");
        index += 1;
        if(index > 10000)
        {
            index = 0;
        }
        emit datar(index);
    }
   emit finished();
 }

worker.h

 #ifndef WORKER_H
 #define WORKER_H

 #include <QObject>

 class worker : public QObject
{
   Q_OBJECT
 public:
  explicit worker(QObject *parent = 0);

 signals:
   void finished();
   void datar(int);

 public slots:
   void process();
 };

#endif // WORKER_H

What i wanted was to update the line edit continusoly from thread such that it doesn't affect the GUI performance. It would be great if you identify the mistake and suggest me the changes to do.


Solution

  • Consider your worker::process implementation...

    void worker::process()
    {
        int index = 0;
        qDebug()<<"In here";
        while(true)
        {
            qDebug("Hello World!");
            index += 1;
            if(index > 10000)
            {
                index = 0;
            }
            emit datar(index);
        }
        emit finished();
     }
    

    It emits the datar signal continuously and without any intervening delays. But the signal emitter and receiver are on different threads meaning the signal will be delivered to the receiver via its event queue. So you are basically saturating the GUI thread's event loop with events from the datar signal.

    Try putting even a slight delay between signals with something like...

    QThread::msleep(10);