Question

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)

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top