updating QLabel in non-GUI thread continuously

南楼画角 提交于 2019-12-02 11:35:57

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

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.

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

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