Qt QObject::killTimer: timers cannot be stopped from another thread


QObject::killTimer: timers cannot be stopped fr another thread

产生原因

1 定时器的事件处理所处的线程和调用定时器操作(方法)的线程不是同一个线程。 2 可以通过 qDebug() << QThread::currentThreadId()打印操作时的线程ID。

解决思路

把定时器的操作(方法:start(), stop())))和事件处理放在同一线程内操作。

解决方法

使用connect(ui>pushButton, SIGNAL(clicked()), w, SLOT(SlotShow()));而不是直接使用w>SlotShow();。

关键代码

1 //mainwindocpp 2 #include "mainwindoh" 3 #include "ui_mainwindoh" 4 #include <QThread> 5 #include <work.h> 6 #include <QDebug> 7 8 MainWindow::MainWindow(QWidget *parent) 9 : QMainWindow(parent) 10 , ui(new Ui::MainWindow) 11 20 21 MainWindow::~MainWindow() 22 28 29 void MainWindow::on_pushButton_2_clicked() 30
1 //workpp 2 #include "work.h" 3 #include <QDebug> 4 #include <QThread> 5 #include <QTimer> 6 7 Work::Work(QObject *parent) : 8 QObject(parent) 9 14 15 Work::~Work() 16 19 20 void Work::SlotShow() 21



上一篇:Qt killtimer 析构函数 Error: timer id is not valid for object timer has not been killed

下一篇:Qt QWidget Must construct a QApplication before a QWidget


Qt
Copyright © 2002-2019 k262电脑网 www.k262.cn 皖ICP备2020016292号
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!QQ:251442993 热门搜索 网站地图