持续更新非GUI线程中的QLabel [英] updating QLabel in non-GUI thread continuously

查看:280
本文介绍了持续更新非GUI线程中的QLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个线程中访问Qt GUI的QLabel的QPixmap ,因为我最终将使用它在QLabel中显示mjpeg流,并且我决定使用QLabel,因为它是最简单的方法
它看起来应该像"Live",并且不会阻塞UI,因此要使用另一个(非GUI)线程.

I access the Qt GUI's QLabel's QPixmap in another thread since I will finally use this to display mjpeg stream in QLabel, and I decided to use QLabel since its the easiest way
It should look like 'Live' and not block the UI thus using another (non-gui) thread.

QLabel中没有任何显示.只有例外QPixmap: It is not safe to use pixmaps outside the GUI thread
任何更好或正确的方法可以做到这一点?
这是另一个线程的PyQt代码:self.theQlabel.setPixmap(QtGui.QPixmap.fromImage(myQimg)

nothing shows up in the QLabel. only the exception QPixmap: It is not safe to use pixmaps outside the GUI thread
any better or correct way to do this ?
here is my PyQt code of another thread: self.theQlabel.setPixmap(QtGui.QPixmap.fromImage(myQimg)

推荐答案

使外部线程发出updatePixmap信号,而不是直接设置像素图.然后在GUI线程中,收听信号并在那时更新pixamp.这样的东西应该可以工作(在C ++中):

Instead of directly setting the pixmap, make the external thread emit an updatePixmap signal. Then in the GUI thread, listen to the signal and update the pixamp at that time. Something like that should work (in C++):

// In the GUI thread:

class YourWidget: QObject {

public:

    YourWidget();

public slots:

    void updatePixmap(const QPixmap& pixmap);

}

YourWidget::YourWidget() {
    // Connect to the signal here:
    QObject::connect(otherThread, SIGNAL(updatePixmap(const QPixmap&)), this, SLOT(updatePixmap(const QPixmap&)));
}

YourWidget::void updatePixmap(const QPixmap& pixmap) {
    // Update the pixmap here in a thread-safe way
}


// In the external thread:

// Emit the signal. The GUI thread will receive it and can then update the pixmap
emit updatePixmap(thePixmap);

这篇关于持续更新非GUI线程中的QLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆