Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

qt - Using QWidget::update() from non-GUI thread

Sometimes my application crashes in QWidget::update() that is performing in non-GUI thread.

I am developing an application in which receives video frames from remote host and display them on QWidget.

For this purpose I use libVLC library that gives to me a decoded image. I receive image in libVLC callback, that is performing in separate libVLC thread. In this callback I'm trying to perform QWidget::update() method. Sometimes application crashes, and callstack is somewhere in this method. Here is the my callback code:

//! Called when a video frame is ready to be displayed, according to the vlc clock. 
//! c picture is the return value from lockCB().

void VideoWidget::displayCB(void* picture)
{
    QImage* image = reinterpret_cast<QImage*>(picture);

    onScreenPixmapMutex_.lock();
    onScreenPixmap_ = QImage(*image);
    onScreenPixmap_.detach();
    onScreenPixmapMutex_.unlock();

    delete image;

    update();
}

I know that GUI operations outside the main thread are not allowed in Qt. But according documentation QWidget::update() just schedules a paint event for processing when Qt returns to the main event loop and does not cause an immediate repaint.

The questtion is: is the rule "GUI operations outside the main thread are not allowed" appliable for QWidget::update()? Does this operation belong to "GUI operations"?

I use Qt 4.7.3, the crash repoduces on Windows 7 and Linux.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check out the Mandelbrot Example. In that example a worker thread is generating an image and passing it to rendering widget with signal/slot mechanism. Use the same method!

Instead of implementing a new updatePixmap() slot as given in the example you can directly connect update () slot of your widget as well.

From your code I can see that you have a mutex to provide concurrent access. So it should be easy to directly use your update slot.

Both methods still use signal/slot mechanism because GUI operations outside the main thread are not allowed in Qt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...