QT 多线程 使用UI

QT 多线程 使用UI直接上代码 qt 的 ui 操作必须在主线程做的 分支线程只能发送消息给主线程进行引导操作 所以平常我们的代码都是直接使用一个线程来调动 UI 但是不同的线程同时需要使用 UI 来显示结果之类的就需要相互协调 如果没有 invoke 之类的方法 可以考虑直接使用 qt 的 Qthread 直接使用 thread 会冲突 1 nbsp nbsp 需要使用 UI 的线程所在的类必须是继承自 Qthread 头文件 includ

直接上代码:

qt的ui操作必须在主线程做的,分支线程只能发送消息给主线程进行引导操作。

所以平常我们的代码都是直接使用一个线程来调动UI,但是不同的线程同时需要使用UI来显示结果之类的就需要相互协调;

如果没有invoke之类的方法,可以考虑直接使用qt 的Qthread;直接使用thread会冲突;

1    需要使用UI的线程所在的类必须是继承自Qthread; 头文件#include

   定义 信号函数

#include 
  
    #include 
   
     #include 
    
      using namespace std; class QTgui :public QThread { Q_OBJECT signals : void sigCurrentImage2(int img); 
     
    
  

2  链接类线程函数(信号)和主界面显示UI的函数(槽)

g1 = new QTgui(this); connect(g1, SIGNAL(sigCurrentImage2(int)), this, SLOT(slot3(int)));

3 在类中重写run函数

void QTgui::run() { while (add == 0) { k += 3; this_thread::sleep_for(std::chrono::milliseconds(timee)); emit sigCurrentImage2(k); } }

4 启动run线程

void QTgui::addd() { add = 0; start(); //std::thread thread1(std::bind(&QTgui::porcess, this)); //tthread = &thread1; //tthread->detach(); }

5 全部代码  在这里:

 

#pragma once #include 
  
    #include 
   
     #include 
    
      using namespace std; class QTgui :public QThread { Q_OBJECT signals : void sigCurrentImage2(int img); public: QTgui(QObject *parent); ~QTgui(); int k; int add; int timee=3; std::thread * tthread; void run(); void addd(); void porcess(); }; #include "QTgui.h" QTgui::QTgui(QObject *parent) : QThread(parent) { k = 0; add = 0; } QTgui::~QTgui() { } void QTgui::run() { while (add == 0) { k += 2; this_thread::sleep_for(std::chrono::milliseconds(timee)); emit sigCurrentImage2(k); } } void QTgui::addd() { start(); //std::thread thread1(std::bind(&QTgui::porcess, this)); //tthread = &thread1; //tthread->detach(); } void QTgui::porcess() { while (add == 0) { k += 2; this_thread::sleep_for(std::chrono::milliseconds(timee)); emit sigCurrentImage2(k); } } #pragma once #include 
     
       #include "ui_threadGui.h" #include"QTgui.h" class threadGui : public QMainWindow { Q_OBJECT public: threadGui(QWidget *parent = Q_NULLPTR); QTgui* g1; QTgui* g2; private: Ui::threadGuiClass ui; private slots: void slot2(); void slot1(); void slot3(int k); }; #include "threadGui.h" threadGui::threadGui(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); g1 = new QTgui(this); /*connect(g1, &QTgui::sigCurrentImage2, [=](int img) { slot3(img); });*/ g2 = new QTgui(this); g2->k = 1; g2->timee = 50; //connect(g2, &QTgui::sigCurrentImage2, [=](int img) { // slot3(img); //}); connect(g1, SIGNAL(sigCurrentImage2(int)), this, SLOT(slot3(int))); connect(g2, SIGNAL(sigCurrentImage2(int)), this, SLOT(slot3(int))); } void threadGui::slot3(int k) { if (k %2 ==0 ||true) { ui.label->setText(QString::number(k)); } else { ui.label_2->setText(QString::number(k)); } } void threadGui::slot1() { g1->addd(); g2->addd(); } void threadGui::slot2() { g1->add = 1; g2->add = 1; ui.label_3->setText(QString::number(g1->k) + QString::number(g2->k)); } 
      
     
    
  

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/224938.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 上午10:48
下一篇 2026年3月17日 上午10:48


相关推荐

  • 文件读写的理论

    文件读写的理论

    2022年1月20日
    41
  • java中static关键字的使用说明

    java中static关键字的使用说明1 static 关键字的用途 在 Java 编程思想 P86 页有这样一段话 static 方法就是没有 this 的方法 在 static 方法内部不能调用非静态方法 反过来是可以的 而且可以在没有创建任何对象的前提下 仅仅通过类本身来调用 static 方法 这实际上正是 static 方法的主要用途 这段话虽然只是说明了 static 方法的特殊之处 但是可以看出 static 关键字的基本作用 简

    2026年3月19日
    2
  • getopt getopts

    getopt getopts一.getopt1.使用getoptoptstringoptionsparametersoptstring定义了命令行有效的选项字母,还定义了哪些选项字需要参数,需要参数的选项后面加:即可;如果指定了一个不在optstring中的选项,默认情况下,getopt命令会产生一条错误消息,如果想要忽略,可以加上-q选项;getoptab:cd-a-bte

    2022年5月4日
    50
  • Java数组删除指定元素

    Java数组删除指定元素//删除数组中其中一个元素publicstaticvoidtestB(){String[]str={“Java”,”C++”,”Php”,”C#”,”Python”};//删除phpList<String>list=newArrayList<String>();for(inti=0;i<str.length;i++){list.add(str[i]

    2022年8月11日
    9
  • vscode注释快捷键失效_代码变成注释快捷键

    vscode注释快捷键失效_代码变成注释快捷键目录1.单行注释2.多行或块注释1.单行注释ctrl+/:C中产生双斜线注释或取消双斜线注释,未在其它语言使用,暂未知//注释类型2.多行或块注释shift+alt+A:C中产生/**/注释或取消/**/注释,未在其它语言使用,暂未知/*注释类型*/…

    2022年8月15日
    8
  • map改变一个字母是什么_map中a的发音音标

    map改变一个字母是什么_map中a的发音音标原题链接给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。示例:输入: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]输出:[ [“ate”,”eat”,”tea”], [“nat”,”tan”], [“bat”]]说明:所有输入均为小写字母。不考虑答案输出的顺序。tclass Solution {public: vector<vector<string>> g

    2022年8月9日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号