代码如下:
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();
}