【Qt】QThread::moveToThread
来源:互联网 发布:深锋网络 编辑:程序博客网 时间:2024/06/10 16:15
myobject.h
#ifndef MYOBJECT#define MYOBJECT#include <QObject>#include <QThread>#include <QDebug>#include <QApplication>class MyObject:public QObject{ Q_OBJECTpublic: MyObject(){} ~MyObject(){}public slots: void first() { qDebug() << QThread::currentThreadId(); } void second() { int i = 0; while(true) { if(i++ < 10) qDebug() << QThread::currentThreadId() << "second()"; else break; } } void three() { for(int i = 0; i< 20000; ++i) { qDebug() <<QThread::currentThreadId()<< " -> three -> " << i; //qApp->processEvents(); } }};#endif // MYOBJECT
dialog.h
#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include "myobject.h"class Dialog : public QDialog{ Q_OBJECTpublic: Dialog(QWidget *parent = 0); ~Dialog();private: MyObject * my;};#endif // DIALOG_Hdialog.cpp
#include "dialog.h"#include <QPushButton>#include <QVBoxLayout>#include "myobject.h"Dialog::Dialog(QWidget *parent) : QDialog(parent){ this->setWindowTitle("moveToThread 事例"); qDebug() << "main" << QThread::currentThreadId(); my = new MyObject; QPushButton *button1 = new QPushButton("1"); QPushButton *button2 = new QPushButton("2"); QPushButton *button3 = new QPushButton("3"); QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(button1); layout->addWidget(button2); layout->addWidget(button3); this->setLayout(layout); connect(button1,SIGNAL(clicked(bool)),my,SLOT(first())); connect(button2,SIGNAL(clicked(bool)),my,SLOT(second())); connect(button3,SIGNAL(clicked(bool)),my,SLOT(three())); QThread *thread = new QThread; my->moveToThread(thread); connect(thread,SIGNAL(finished()),my,SLOT(deleteLater())); connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater())); thread->start();}Dialog::~Dialog(){}
main.cpp
#include "dialog.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); Dialog w; w.show(); return a.exec();}
0 0
- 【Qt】QThread::moveToThread
- Qt多线程两种方式优劣,继承QThread还是MoveToThread
- qt的QThread消息归属和MoveToThread的作用
- QThread with movetothread
- Qt moveToThread
- Qt moveToThread
- Qt moveToThread
- void QObject::moveToThread(QThread * targetThread)
- qt thread with movetothread
- 在Qt使用moveToThread()
- Qt moveToThread使用说明
- Qt 线程 ---movetothread
- Qt 多线程之MoveToThread()
- Qt线程之moveToThread
- QT QThread
- 一种使用QThread线程的新方法QObject::moveToThread
- 一种使用QThread线程的新方法QObject::moveToThread
- 一种使用QThread线程的新方法QObject::moveToThread
- POJ 3122 Pie(二分)
- Scheme 语言概要(上)
- CODEVS 1021 玛丽卡 最短路模板题
- 手机淘宝构架演化实践
- uboot 引导kernel杂谈
- 【Qt】QThread::moveToThread
- Scheme 语言概要(下)
- 鼠标马赛克图像部分区域
- 黑马程序员--对象的存储细节
- android获取手机ip地址wifi状态与非wifi状态
- hive配置问题异常 Relative path in absolute URI
- 零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)
- const, static, extern, typedef
- hive-学习笔记