16,816
社区成员




#ifndef WORK_H
#define WORK_H
#include <QObject>
class Work : public QObject
{
Q_OBJECT
public:
explicit Work(QObject *parent = nullptr);
public slots:
void do_work(const QString& parameter);
signals:
void result_ready(const QString& result);
};
#endif // WORK_H
#include <QThread>
#include <QtDebug>
#include "work.h"
Work::Work(QObject *parent) : QObject(parent)
{
}
void Work::do_work(const QString ¶meter)
{
qDebug()<< parameter << "start";
qDebug()<< (quint64) QThread::currentThreadId() << endl;
qDebug()<< parameter << "end";
}
void Thread_test::create_new_thread()
{
Work* work(new Work());
work->do_work("test");
qDebug()<< "------------------------------------------------" << endl;
// 创建新线程thread,将worker对象移动到thread
QThread* thread(new QThread());
work->moveToThread(thread);
connect(this, &Thread_test::singal_thread, work, &Work::do_work);
thread->start();
// 001
emit singal_thread("emit signal/slot");
// 002
work->do_work("calls do_work");
qDebug()<<"=============================================" << endl;
QThread::sleep(3);
qDebug()<< "in create_new_thread function" << (quint64) currentThreadId() << endl;
qDebug()<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`" << endl;
}