线程中的QTimer

时间:2018-10-07 11:41:24

标签: multithreading qt signals-slots qtimer

我只想在GUI上单击按钮时发出2个信号和Qtimer超时。

尽管2个信号/插槽有效,QTimer的信号/插槽不起作用,但是timeoutout插槽永不起作用。 甚至没有错误。

GUI(DIALOG.CPP)

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    fduthread = new fdustatus(this);
    connect(fduthread,SIGNAL(NumberChanged(int)),this,SLOT(onNumberChanged(int)));
    connect(fduthread,SIGNAL(nameChange(QString)),this,SLOT(onNameChanged(QString)));
}

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

void Dialog::onNumberChanged(int number)
{
    ui->label->setText(QString::number(number));
}

void Dialog::onNameChanged(QString s)
{

    ui->label_2->setText(s);

}

void Dialog::on_pushButton_clicked()
{
    fduthread->start();
    fduthread->stop=false;
}

void Dialog::on_pushButton_2_clicked()
{
    ui->label_2->setText("");
    fduthread->stop=true;
}

这就是我的想法

#include "fdustatus.h"
#include<QMutex>
#include<QTimer>
#include<QDebug>

fdustatus::fdustatus(QObject *parent):QThread(parent)
{

}
void fdustatus::run()
{
   mytimer = new QTimer();
   mytimer->setInterval(10);
    connect( mytimer,SIGNAL(timeout()),this,SLOT(whentimeout()));
     mytimer->start();
    for(int i =0;i<100;i++)
    {
        QMutex mutex;
        mutex.lock();
        if(this->stop)break;
        mutex.unlock();

        emit NumberChanged(i*10);

        emit nameChange(getstring());
        this->msleep(100);
   }

}
QString fdustatus::getstring()
{

    QString networkport;
    networkport.append("Alarm Active");
    return networkport;
}
void fdustatus::whentimeout()
{
    qDebug() << "timer ended from thread..";

}

0 个答案:

没有答案