-1

I have a program that is meant to find a certain amount of numbers with odds/evens. I keep track of this and display in in my GUI from a worker thread. This works, but while it is working the program freezes. The program is still calculating, but it stops updating the GUI. Here is my relevant code:

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QThread>

class CounterThread : public QThread
{
    Q_OBJECT
public:
    CounterThread();
    void run();
signals:
    void setProgress(int value);
    void setOdd0(int value);
    void setOdd1(int value);
    void setOdd2(int value);
    void setOdd3(int value);
    void setOdd4(int value);
    void setOdd5(int value);
    void setOdd6(int value);
};

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    CounterThread *cThread;
};
#endif // MAINWINDOW_H

MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qDebug>
#include <QLCDNumber>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cThread = new CounterThread();
    connect(cThread, SIGNAL(setOdd0(int)), ui->lcdNumberodd0, SLOT(display(int)));
    connect(cThread, SIGNAL(setOdd1(int)), ui->lcdNumberodd0, SLOT(display(int)));
    connect(cThread, SIGNAL(setOdd2(int)), ui->lcdNumberodd0, SLOT(display(int)));
    connect(cThread, SIGNAL(setOdd3(int)), ui->lcdNumberodd0, SLOT(display(int)));
    connect(cThread, SIGNAL(setOdd4(int)), ui->lcdNumberodd0, SLOT(display(int)));
    connect(cThread, SIGNAL(setOdd5(int)), ui->lcdNumberodd0, SLOT(display(int)));
    connect(cThread, SIGNAL(setOdd6(int)), ui->lcdNumberodd0, SLOT(display(int)));
    connect(cThread, SIGNAL(setProgress(int)), ui->progressBar, SLOT(setValue(int)));
}

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

void MainWindow::on_pushButton_clicked()
{
    ui->progressBar->setMaximum(13983816);
    cThread->start();
    ui->progressBar->setValue(ui->progressBar->maximum());
}


CounterThread::CounterThread()
{

}

void CounterThread::run()
{
    int odd0 = 0;
    int odd1 = 0;
    int odd2 = 0;
    int odd3 = 0;
    int odd4 = 0;
    int odd5 = 0;
    int odd6 = 0;
    for (int x = 99999; x < 999999; x++)
    {

        int oddNumbers = 0;
        for(int i = 0; i < QString::number(x).length(); i++)
        {
            if (QString(QString::number(x)[i]).toInt() % 2 != 0)
                oddNumbers++;
        }
        setProgress(x);
        switch (oddNumbers)
        {
        case 0:
            odd0++;
            setOdd0(odd0);
            break;
        case 1:
            odd1++;
            setOdd1(odd1);
            break;
        case 2:
            odd2++;
            setOdd2(odd2);
            break;
        case 3:
            odd3++;
            setOdd3(odd3);
            break;
        case 4:
            odd4++;
            setOdd4(odd4);
            break;
        case 5:
            odd5++;
            setOdd5(odd5);
            break;
        case 6:
            odd6++;
            setOdd6(odd6);
            break;
        }
    }
}
Community
  • 1
  • 1
Nicholas Johnson
  • 1,012
  • 2
  • 12
  • 35
  • `(QString(QString::number(x)[i]).toInt() % 2 != 0)` think more about this part. – Inline Aug 22 '16 at 04:43
  • Your code has no `main()`, so linking fails and we can't reproduce the symptoms you describe. Please learn how to create a [mcve], then [edit] your question to add the missing code. – Toby Speight Aug 22 '16 at 15:13

1 Answers1

2

Your worker thread's run() function modifies an object owned by the main thread (e.g. the UI thread.) The many calls to setProgress() will quickly fill the event queue on the GUI and cause other events to not be serviced responsively.

The correct way to set up worker threads in Qt has been discussed elsewhere on Stackoverflow, for example:

Qt4: while loop in seperate thread blocks GUI

Community
  • 1
  • 1
Chris
  • 474
  • 2
  • 8
  • 22