QTimer不会触发(触发)超时

时间:2015-07-07 15:08:39

标签: timeout qthread qtimer qeventloop

我想使用Qtimer暂停执行给定的代码块,而在另一个线程中它会执行其他操作。我用qeventloop quit连接了线程的超时,但问题是,没有调用超时。当另一个发生发生时,超时被神奇地触发,或者如果我添加另一个连接,则也会触发超时。我想我错过了使用qtimer,qeventloop和qthread的一些东西。有人可以帮忙吗?我提取了用于测试的基本代码并将其放在此处:

的main.cpp

#include "widget.h"
#include <QApplication>
#include "tim.h"
#include <QThread>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w=new Widget();
tim *t=new tim();
QThread *thread=new QThread();
t->moveToThread(thread);
thread->start();
        QThread::connect(w,SIGNAL(signalDoIt()),t,SLOT(slotDoIt()),Qt::QueuedConnection);
QThread::connect(w,SIGNAL(signalQuitTimer()),t,SLOT(slotQuitTimer()),Qt::QueuedConnection);
    QThread::connect(t,SIGNAL(signalSetText(QString)),w,SLOT(slotSetText(QString)),Qt::QueuedConnection);
w->show();
return a.exec();
}

tim.h

#ifndef TIM_H
#define TIM_H

#include <QObject>
#include<QTimer>
#include<QTime>
#include<QEventLoop>
#include<QThread>
#include<QDebug>
class tim : public QObject
{
Q_OBJECT
public:
tim();
~tim();
signals:
void signalSetText(QString);
public slots:
void slotDoIt();
void slotQuitTimer();
void slotShowTime();
private:
QTimer *trainingTimer;
QEventLoop loopTrainingWait;
QTime time;
};

#endif // TIM_H

tim.cpp

#include "tim.h"

tim::tim()
{
qDebug()<<"constructor";
trainingTimer=new QTimer(this);
trainingTimer->setTimerType(Qt::PreciseTimer);
trainingTimer->setSingleShot(true);
QThread::connect(trainingTimer,SIGNAL(timeout()),&loopTrainingWait,SLOT(quit()));
// QThread::connect(trainingTimer,SIGNAL(timeout()),this,SLOT(slotShowTime())); //to uncomment all works, but withou this, it does not
}
void tim::slotDoIt()
{

trainingTimer->start(5000);
time.start();
loopTrainingWait.exec();
QString text(QString::number(loopTrainingWait.isRunning())+"  "+ QString::number(time.elapsed()));
qDebug()<<text;
emit signalSetText(text);

}
void tim::slotShowTime()
{
QString text(QString::number(loopTrainingWait.isRunning())+" slot "+     QString::number(time.elapsed()));
qDebug()<<text;
emit signalSetText(text);
}
void tim::slotQuitTimer()
{
if(loopTrainingWait.isRunning())
    loopTrainingWait.quit();
if(trainingTimer->isActive())
    trainingTimer->stop();
}
tim::~tim()
{

}

// gui进行测试 widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();
public slots:
void slotSetText(QString text);
signals:
void signalDoIt();
void signalQuitTimer();
private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

void on_pushButton_3_clicked();

private:
Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
void Widget::slotSetText(QString text)
{
ui->label->setText(text);
}
Widget::~Widget()
{
delete ui;
}

void Widget::on_pushButton_clicked()
{
this->close();
}

void Widget::on_pushButton_2_clicked()
{
emit signalDoIt();
}

void Widget::on_pushButton_3_clicked()
{
emit signalQuitTimer();
}

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,最终我在“公共”和“:”之间写了SLOT,然后就可以了

答案 1 :(得分:0)

问题在于静态qeventloop,使这个动态

loopTrainingWait = new QEventLoop(this);

与父母一起解决问题