Qt5.9 MoveToThread的使用20200730

代码如下:

MyThread.h

#pragma once

#include <QObject>
#include <QDebug>
#include <QThread>

class MyThread : public QObject
{
	Q_OBJECT

public:
	MyThread();
	~MyThread();

public slots://3个槽函数
	void first();
	void second();
	void three();

signals://3个信号
	void sig_sendfirst();
	void sig_sendsecond();
	void sig_sendThird();
};

MyThread.cpp 

#include "MyThread.h"

MyThread::MyThread()
{
}

MyThread::~MyThread()
{
}

void MyThread::first()
{
	qDebug() << "first" << QThread::currentThreadId();
	sig_sendfirst();
}

void MyThread::second()
{
	qDebug() << "second" << QThread::currentThreadId();
	sig_sendsecond();
}

void MyThread::three()
{
	qDebug() << "three" << QThread::currentThreadId();
	sig_sendThird();
}

MovetoThreadTest.h 

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_MovetoThreadTest.h"
#include "MyThread.h"

class MovetoThreadTest : public QMainWindow
{
	Q_OBJECT

public:
	MovetoThreadTest(QWidget *parent = Q_NULLPTR);
	~MovetoThreadTest();

private:
	Ui::MovetoThreadTestClass ui;
	MyThread * m_pMyThread;

public slots:
	void onSelfPushed();
	void onExitPushed();

	void slot_myfirst();
	void slot_mysecond();
	void slot_mythird();

	void slot_recivefirst();
	void slot_recivesecond();
	void slot_recivethird();

signals:
	void  sig_firt();
	void  sig_second();
	void  sig_third();
};

 MovetoThreadTest.cpp

#include "MovetoThreadTest.h"

MovetoThreadTest::MovetoThreadTest(QWidget *parent)
	: QMainWindow(parent)
{
	qDebug() << "main" << QThread::currentThreadId();
	ui.setupUi(this);

	m_pMyThread = new MyThread;
	QThread *thread = new QThread;//创建一个新的线程
	m_pMyThread->moveToThread(thread);//MovetoThread
	thread->start();//启动新的线程
	connect(thread, SIGNAL(started()), m_pMyThread, SLOT(first()));
	//用不同的方式调用mythread的方法
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(slot_myfirst()), Qt::AutoConnection);
	connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(slot_mysecond()), Qt::QueuedConnection);
	connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(onSelfPushed()));
	connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(onExitPushed()));
	connect(ui.pushButton_3, SIGNAL(clicked()), m_pMyThread, SLOT(three()), Qt::QueuedConnection);

	connect(this, SIGNAL(sig_firt()), m_pMyThread, SLOT(first()), Qt::QueuedConnection);
	connect(this, SIGNAL(sig_second()), m_pMyThread, SLOT(second()), Qt::QueuedConnection);
	connect(this, SIGNAL(sig_third()), m_pMyThread, SLOT(three()), Qt::QueuedConnection);
}

MovetoThreadTest::~MovetoThreadTest()
{

}

void MovetoThreadTest::onSelfPushed()
{
	qDebug() << "onSelfPushed" << QThread::currentThreadId();
}
void MovetoThreadTest::onExitPushed()
{
	qDebug() << "onExitPushed" << QThread::currentThreadId();
}

void MovetoThreadTest::slot_myfirst()
{
	qDebug() << "slot_myfirst" << QThread::currentThreadId();
	emit sig_firt();
}
void MovetoThreadTest::slot_mysecond()
{
	qDebug() << "slot_mysecond" << QThread::currentThreadId();
	m_pMyThread->second();
}
void MovetoThreadTest::slot_mythird()
{

}

void MovetoThreadTest::slot_recivefirst()
{
	qDebug() << "slot_recivefirst" << QThread::currentThreadId();
}
void MovetoThreadTest::slot_recivesecond()
{
	qDebug() << "slot_recivesecond" << QThread::currentThreadId();
}
void MovetoThreadTest::slot_recivethird()
{
	qDebug() << "slot_recivethird" << QThread::currentThreadId();
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值