qt使用moveToThread,开启线程。demo模版

cpp 复制代码
#ifndef THREAD_H
#define THREAD_H

#include <QObject>

class Thread :public QObject
{
    Q_OBJECT
public:
    Thread();
    ~Thread();
    void Thread_Fun(void);
};

#endif // THREAD_H
cpp 复制代码
#include "outputlogthread.h"
#include <QDebug>
#include <QThread>
Thread::Thread()
{
    qDebug()<<"Thread构造函数ID:"<<QThread::currentThreadId();
}

Thread::~Thread()
{

}

void Thread::Thread_Fun()
{
    qDebug()<<"子线程功能函数ID:"<<QThread::currentThreadId();
}

主线程

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include"outputlogthread.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


signals:
    void ToThread(); // 信号
public:
    QThread *Thread_Test;
    Thread *thread_class;
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QThread>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Thread_Test = new QThread;
    thread_class = new Thread;
    connect(this,&MainWindow::ToThread,thread_class,&Thread::Thread_Fun);
    thread_class->moveToThread(Thread_Test);
    //下面两个是一样的,都可以调用
    Thread_Test->start();
    // emit ToThread();


}

MainWindow::~MainWindow()
{
    delete ui;
}
相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner10 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能14 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G14 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt