Qt-非UI线程与UI线程通信

利用槽函数,在非UI线程发送信号,UI线程进行接收。

线程类

#pragma once
#include <qthread.h>
class TThread : public QThread
{
	Q_OBJECT
public:
	TThread();
	~TThread();
signals:
	void sendValue(int i);

public:
	virtual void run();

};

线程类实现

#include "TThread.h"
TThread::TThread()
{
}
TThread::~TThread()
{
}

void TThread::run()
{
	for (int i = 0; i < 100; i++)
	{
		emit sendValue(i);
		msleep(25);
	}
}

在相关UI类中创建槽:

public slots:
    	//更改进度条的值
    	void changeSilderValue(int i);

创建线程,绑定信号和槽

_thread = new TThread();
connect(_thread, SIGNAL(sendValue(int)), this,SLOT(changeSilderValue(int)));
void QtGuiApplication2::changeSilderValue(int i)
{
	ui.horizontalSlider->setValue(i);
}

在需要的时候启动线程即可

_thread->start();
posted @ 2022-06-07 08:00  清楚xc  阅读(189)  评论(0)    收藏  举报