0

我的 Qt 线程和它的信号有问题。我正在创建一个名为 dworker 的 QObject 并将其移动到 QThread ,然后单击我的开始按钮启动线程。它基本上只是用一些数字更新 GUI,当我按下停止按钮时应该停止。好吧,它停止了,但是“qDebug()<<”线程停止=“<<停止;” in dworker 没有被调用,我无法再次启动它。信号已触发,但插槽方法未执行。

主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

namespace GLOBAL
{
    Settings mSettings;
    Data mData;
}

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

    dthread = new QThread(this);
   dworker = new Dworker();
   dworker->moveToThread(dthread);

    connect(dworker, SIGNAL(set_values(double,double,double,double,double,double)),
            this, SLOT(slot_set_values(double,double,double,double,double,double)));
    connect(ui->startButton, SIGNAL(clicked()), dworker, SLOT(slot_process()));
    connect(ui->stopButton, SIGNAL(clicked()), dworker, SLOT(slot_end_process()));

    dthread->start();
}

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

void MainWindow::slot_set_values(double ptm_temp, double ptm_hv, double heat_temp, double nomtemp, double current, double voltage)
{
    ui->pmtValueLabel->setText(QString::number(ptm_temp));
    ui->hvValueLabel->setText(QString::number(ptm_hv));
    ui->heatValueLabel->setText(QString::number(heat_temp));
    ui->nomValueLabel->setText(QString::number(nomtemp));
    ui->currenValueLabel->setText(QString::number(current));
    ui->vValueLabel->setText(QString::number(voltage));

    //qDebug() <<"set_values SLOT " <<ptm_temp<<" "<<ptm_hv<<" "<<heat_temp<<" "<<nomtemp<<" "<<current<<" "<<voltage;

}

void MainWindow::on_startButton_clicked()
{

}

void MainWindow::on_stopButton_clicked()
{
    dworker->stop = true;
    qDebug() << "send stop";
}

dworker.cpp:

#include "dworker.h"

using namespace GLOBAL;

QMutex mutex;

Dworker::Dworker(QObject *parent) :
    QObject(parent)
{

}

void Dworker::slot_process()
{
    stop = false;

    while (true)
    {

        mutex.lock();
        if(stop) break;
        mutex.unlock();

        qsrand(QDateTime::currentDateTime().toTime_t());
        mData.set_pmt_temp(qrand()%100);
        mData.set_pmt_hv(qrand()%100);
        mData.set_heat_opt_temp(qrand()%100);
        mData.set_heat_nominal_temp(qrand()%100);

        double pmt_tmp = mData.get_pmt_temp();
        double hv = mData.get_pmt_hv();
        double heat_temp = mData.get_heat_opt_temp();
        double heat_nom = mData.get_heat_nominal_temp();

        emit set_values(pmt_tmp,hv,heat_temp,heat_nom,0,0);

        QThread::msleep(1000);
        qDebug() <<"Thread SIGNAL " <<pmt_tmp<<" "<<hv<<" "<<heat_temp<<" "<<heat_nom;
    }
}

void Dworker::slot_end_process()
{
    mutex.lock();
    stop = true;
    mutex.unlock();

    qDebug() << "thread stopping = " << stop;
}
4

1 回答 1

0

很多问题!

您在 connect 中使用默认值Qt::AutoConnection。这意味着如果信号通过目标线程的事件循环在线程之间传递,请参阅doc。这就是为什么你不能停止你的函数,你的事件循环在slot_process()运行时永远不会获得控制。要修复它,像这样更改连接(使用Qt::DirectConnection):

connect(ui->stopButton, SIGNAL(clicked()), 
        dworker, SLOT(slot_end_process())
        Qt::DirectConnection);

另一个问题:您错误地将互斥锁锁定在 中slot_process(),请注意 break 将跳过解锁并且互斥锁将永久保持锁定,像这样修复它:

{
    QMutexLocker locker(&mutex);
    if(stop) break;
} // braces are important
于 2013-08-14T12:22:40.587 回答