QT 多线程 使用UI

直接上代码:

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

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

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

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

   定义 信号函数

#include <QObject>
#include<qthread.h>
#include<thread>
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 <QObject>
#include<qthread.h>
#include<thread>
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 <QtWidgets/QMainWindow>
#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));
}

 

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Qt中,多线程更新UI是一个常见的问题。根据引用\[2\]和引用\[3\]的代码片段,可以看出一种解决方案是使用信号和槽机制。在主线程中创建一个QWidget对象,并在该对象的构造函数中创建一个QTextEdit对象。然后,创建一个自定义的Thread对象,并将其与QTextEdit对象的append槽函数连接起来。在Thread对象的run函数中,可以通过发射信号的方式将需要更新的字符串传递给QTextEdit对象的append槽函数,从而实现在非GUI线程中更新UI。具体代码如下: ```cpp class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QTextEdit *m_textEdit; Thread *m_thread; }; Widget::Widget(QWidget *parent) : QWidget(parent) { m_textEdit = new QTextEdit(this); QHBoxLayout *layout = new QHBoxLayout(this); layout->addWidget(m_textEdit); setLayout(layout); m_thread = new Thread(); connect(m_thread, SIGNAL(sendString(QString)), m_textEdit, SLOT(append(QString))); m_thread->start(); } Widget::~Widget() { } ``` 在上述代码中,Widget类继承自QWidget,并包含一个QTextEdit成员变量m_textEdit和一个Thread成员变量m_thread。在Widget的构造函数中,创建了一个水平布局,并将m_textEdit添加到布局中。然后,创建了一个Thread对象,并将其sendString信号与m_textEdit的append槽函数连接起来。最后,启动Thread对象的运行。 通过这种方式,可以在非GUI线程中通过发射信号的方式更新UI,从而实现多线程更新UI的需求。 #### 引用[.reference_title] - *1* *2* *3* [qt 子线程(多线程)更新gui的几种方法](https://blog.csdn.net/u011555996/article/details/124461511)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值