即使使用QThread,QT程序也会停止

时间:2016-08-22 01:24:21

标签: c++ qt qthread qtgui

我有一个程序可以找到一定数量的赔率/赔率数字。我跟踪这个并从我的工作线程中显示在我的GUI中。这是有效的,但它正在工作程序冻结。该程序仍在计算,但它停止更新GUI。这是我的相关代码:

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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的工作线程的run()函数修改主线程拥有的对象(例如UI线程)。对setProgress()的多次调用将快速填充GUI上的事件队列,并导致其他事件无法响应地进行处理。

在Qtt中设置工作线程的正确方法已在Stackoverflow的其他地方讨论过,例如:

Qt4: while loop in seperate thread blocks GUI