updating QLabel in non-GUI thread continuously

牧云@^-^@ 提交于 2019-12-02 19:04:31

问题


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.

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)


回答1:


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);



回答2:


I think that it might be dangerous to even create a QPixmap in anything other than the GUI thread. You might want to consider passing a QImage and converting it to a QPixmap in the main thread. I can't find a direct reference to support this assertion, but

http://developer.qt.nokia.com/doc/qt-4.8/thread-basics.html

hints that

All widgets and several related classes, for example QPixmap, don't work in secondary threads.

The email thread at

http://lists.trolltech.com/qt-interest/2008-11/msg00534.html

also seems to agree with me.




回答3:


Create RAW DirectX/OpenGL OS context into this QLabel's winId() and do whatever you want. I think, this is the best way for high performance HD video, and the ONLY way :) sometimes you just HAVE to use RAW things to achieve the maximum performance and touch the iron in your computer :)



来源:https://stackoverflow.com/questions/8701781/updating-qlabel-in-non-gui-thread-continuously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!