首页 > 其他分享 >qt 多线程 moveToThread 的一个骚操作

qt 多线程 moveToThread 的一个骚操作

时间:2023-03-23 13:56:15浏览次数:44  
标签:moveToThread 多线程 qt deprecated MainWindow ui include mainwindow

moveToThread  相当于是一个多线程的阻塞函数,

本案例可多次点击按钮,多次触发,这个信号触发类似于内部建立一个队列,处理函数会按照顺序处理信号

 

test_moveToThread.pro

#-------------------------------------------------
#
# Project created by QtCreator 2023-03-23T11:59:07
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = test_moveToThread
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

 

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

 

 

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>


#include <QDebug>
#include <QThread>
class WorkThread: public QObject
{
    Q_OBJECT
signals:
    void sigDowork(QString info);
public slots:
    void doWork(QString info){
        qDebug() << "线程id 1:" << QThread::currentThreadId();
        emit sigDowork(info);
    }

};


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
signals:
    void sigSendInfo(QString info);
    void sigQthreadShowUiInfo(QString key="",QString info="");

private:
    //void showInfo(QString info){}

private slots:
    void testqthread(QString info);
    void ShowUiInfo(QString key="",QString info="");

private:
    Ui::MainWindow *ui;

    WorkThread* worker;
    QThread* qthread;
};

#endif // MAINWINDOW_H

 

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    worker = new WorkThread();
    qthread = new QThread(this);
    worker->moveToThread(qthread);

    connect(ui->btn__f3, &QPushButton::clicked, this,[=](){
        qDebug() << "线程id 2:" << QThread::currentThreadId();
        ShowUiInfo("debug","put task");
        emit sigSendInfo("test sigSendInfo....");
    });//通过线程开启来触发定时器开启
    connect(this, &MainWindow::sigSendInfo, worker,&WorkThread::doWork);//通过线程开启来触发定时器开启
    connect(worker, &WorkThread::sigDowork, this, &MainWindow::testqthread, Qt::DirectConnection);//信号发出后立即调用槽函数,槽函数在发出信号的线程中执行
    connect(qthread, &QThread::finished, qthread, &QObject::deleteLater);//线程结束,自动关闭线程
    qthread->start();

    connect(this, &MainWindow::sigQthreadShowUiInfo, this, &MainWindow::ShowUiInfo);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::testqthread(QString info)
{
    //qDebug() << "线程id 3:" << QThread::currentThreadId();
    //ui->data_reciveInfo->appendPlainText(info);//直接调用可能导致出错,可以使用全局变量,或者信号的方式进行数据显示

    qDebug()<<"start...val:"<< info << "线程id 3:" << QThread::currentThreadId();
    emit sigQthreadShowUiInfo("debug",info);
    emit sigQthreadShowUiInfo("debug","start worker");
    QThread::sleep(5);//5秒
    qDebug()<<"end  ..." << "线程id 3:" << QThread::currentThreadId();
    emit sigQthreadShowUiInfo("debug","stop  worker");
}

void MainWindow::ShowUiInfo(QString key, QString info)
{
    ui->data_reciveInfo->appendPlainText(info);
}

 

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>707</width>
    <height>430</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0" colspan="2">
     <widget class="QPlainTextEdit" name="data_reciveInfo"/>
    </item>
    <item row="1" column="0">
     <spacer name="horizontalSpacer">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>529</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item row="1" column="1">
     <widget class="QPushButton" name="btn__f3">
      <property name="minimumSize">
       <size>
        <width>151</width>
        <height>41</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
        <width>201</width>
        <height>16777215</height>
       </size>
      </property>
      <property name="text">
       <string>测试</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>707</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

运行结果

 

标签:moveToThread,多线程,qt,deprecated,MainWindow,ui,include,mainwindow
From: https://www.cnblogs.com/RYSBlog/p/17247188.html

相关文章

  • Qt Qwt与QCustomPlot
    一、QWT简介QWT,即QtWidgetsforTechnicalApplications,是一个基于LGPL版权协议的开源项目,可生成各种统计图,是为具有技术专业背景的程序提供GUI组件和一组实用类,其......
  • Qt Qwt、QChart、QCustomPlot比较
    美观方面:Qcustomplot≈Qchart>QwtQcustomplot界面简洁大方;Qchart界面华丽优美,其中QChart中绘制的线条处不能使用鼠标,所以在数据量大的时候会引响鼠标的缩放、移动图表......
  • Qt+MySql开发笔记:Qt5.9.3的mingw32版本编译MySql8版本驱动并Demo连接数据库测试
    前言  之前特定的mysql版本msvc版本已经调通了,但是为了更好的跨平台,所以选择用mingw32版本,于是需要编译mysql驱动的mingw32版本的驱动库,以便提供给qt连接mysql使用。<br......
  • Qt QtWidget使用Material风格的组件库
    一、qt-material-widget组件库介绍该组件库拥有炫酷的Material风格的组件,并且该组件库基于QtWidget开发的,目前实现了大约20个Material风格的组件,下面教大家如何编译该组件......
  • 爬虫进阶之多线程爬虫问题详解
    大多数正常人在下载图片的时候都是一个一个点击保存,图片越多花费的时间越多,大大的降低了工作效率。如果是学了爬虫的,一定会想到多线程来自动下载保存图片。多线程介绍:多......
  • esp-idf 5.0 mqtt 华为云连接示例
    1.订阅和发布topic设备消息相关Topic$oc/devices/{device_id}/sys/messages/up设备消息上报$oc/devices/{device_id}/sys/messages/down平台下发消息给设备......
  • #创作者激励#物联网协议MQTT
    1.MQTT简介  MQTT(消息队列遥测传输)是ISO标准(ISO/IECPRF20922)下基于发布/订阅范式的消息协议。它工作在TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟......
  • Qt QXlsx(访问Excel)
    再Qt中已经有了QAxObject来访问Excel,但访问的是微软的com,只能访问正版的Excl中的.xls//xlsx,而且使用起来及其不方便(本人亲测)。在这里使用QXlsx,能更简单的访问Excel数......
  • Qt QXlsx基本使用
     1、概述QXlsx是一个可以读写Excel文件的库。不依赖office以及wps组件,可以在Qt5支持的任何平台上使用;使用方式:1.QXlsx可以编译为静态库库使用(可以提升项目编译速度......
  • QT Creator 安装教程
    Qt介绍:  Qt是一个跨平台的C++开发库。主要用来绘制漂亮的界面,其他还包含很多其它功能,如多线程、访问数据库、图像处理、音频视频处理、网络通信、文件操作等。Qt......