首页| 论坛| 消息

回复: Qt的多线程是不是不能使用QTimer
#16 回 wcrqpdb 的帖子 [angelus 11-08 09:15]
wcrqpdb:要怎么写,麻烦写一下,上面有原程序
 (2019-11-07 19:38) 
写个独立的处理类,然后在里边按平时的习惯定义qtimer,和处理任务,然后把这个类实例化到run函数内,开启线程循环,至于主线程和次线程通讯,直接信号和thread中的连接上就行,这种信号通知属于跨线程信号槽,异步操作
#17 回 angelus 的帖子 [wcrqpdb 11-09 16:26]
angelus:写个独立的处理类,然后在里边按平时的习惯定义qtimer,和处理任务,然后把这个类实例化到run函数内,开启线程循环,至于主线程和次线程通讯,直接信号和thread中的连接上就行,这种信号通知属于跨线程信号槽,异步操作 (2019-11-08 09:15) 
按照你的方法还是报 QObject::startTimer: Timers cannot be started from another thread
#18 [clickto 11-09 23:53]
在run的while里sleep啊,达到timer的效果
#19 [yanwuyue 11-11 09:42]
继承的不要带this,直接qtimer(),并且实例化的k也只能在run里面使用或者信号控制
#20 [巫添良 11-11 22:49]
up_timer = new QTimer;
up_timer->setInterval(1000);
up_timer->start();
up_timer->moveToThread(&up_img_Thread);//定时器移到子线程中
connect(up_timer, SIGNAL(timeout()), this, SLOT(up_model_file()), Qt::DirectConnection);
connect(this, SIGNAL(stop_up_timer()), up_timer, SLOT(stop()));
connect(this,SIGNAL(start_up_timer()),up_timer,SLOT(start()));
up_img_Thread.start();
这样写 定时器的槽函数就是在子线程中执行的

<< 2 3 4 5 6 >> (4/7)

回复 发表
主题 版块