QObject::killTimer: timers cannot be stopped from another thread
产生原因
定时器的事件处理所处的线程和调用定时器操作(方法)的线程不是同一个线程。
可以通过 qDebug() << QThread::currentThreadId()打印操作时的线程ID。
解决思路
把定时器的操作(方法:start(), stop())))和事件处理放在同一线程内操作。
解决方法
使用connect(ui->pushButton, SIGNAL(clicked()), w, SLOT(SlotShow()));
而不是直接使用w->SlotShow();
。
关键代码
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include <work.h>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
w = new Work(); //这里不能指定父对象,如果指定了父对象,moveToThread将无法移动w对象内成员所属的线程。
t = new QThread(this);
w->moveToThread(t); //这里会把对象w及其w中的成员移动到线程t中(在这之前w中的成员属于主线程)。
connect(ui->pushButton, SIGNAL(clicked()), w, SLOT(SlotShow())); //正确用法
t->start();
qDebug() << QThread::currentThreadId() << "MainWindow";
}
MainWindow::~MainWindow()
{
t->quit();
t->wait();
t->deleteLater(); //这里由于对象t所维护的线程和主线程不属于同一线程,应该使用deleterLater进行释放。
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
// w->SlotShow();//错误用法
}
//work.cpp
#include "work.h"
#include <QDebug>
#include <QThread>
#include <QTimer>
Work::Work(QObject *parent) :
QObject(parent)
{
qDebug() << QThread::currentThreadId() << "Work";
m_t = new QTimer(this);
m_t->start();
}
Work::~Work()
{
m_t->deleteLater();
}
void Work::SlotShow()
{
std::this_thread::sleep_for(std::chrono::seconds(5));
m_t->stop();
qDebug() << QThread::currentThreadId() << "SlotShow";
}