绘画QWidget外部GUI线程的问题

编程入门 行业动态 更新时间:2024-10-19 18:22:27
本文介绍了绘画QWidget外部GUI线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个应用程序,其中我想从远程主机连续接收图像,并在我的屏幕上显示它们。为此我遵循给定的策略 1)我有一个主要的QWidget对象包含QImage它(工作正常) 2)从远程主机接收的图像绘在QImage对象,这项工作使用QPainter在工作线程中完成。 (工作正常) 3)但问题是图像不更新QWidget,除非我调整窗口小部件的大小,因为重绘事件被调用QWidget ...现在如果我从工作线程重新绘制QWidget它会给出错误QPixmap:在GUI线程之外使用pixmaps不安全和应用程序崩溃。

I am developing an appliation in which I want to continuously receive images from remote host and display them on my screen. for this I am following the given strategy 1) I have a main QWidget object which contains the QImage on it (works fine) 2) Images received from remote host are painted on QImage object, this work is done in a worker thread using QPainter. (works fine) 3) but the problem is that the image is not updated on QWidget, unless I resize the widget, because the repaint event is called for QWidget... Now if i repaint the QWidget from the worker thread it gives error "QPixmap: It is not safe to use pixmaps outside the GUI thread".. and application crashes.

有关这方面的任何帮助吗?

Any help regarding this?

推荐答案

使用 QueuedConnection 发出工作线程的信号

Emit a signal from the worker thread with a QueuedConnection or post an update event (QPaintEvent) to the widget from the worker thread.

//--------------Send Queued signal--------------------- class WorkerThread : public QThread { //... signals: void updateImage(); protected: void run() { // construct QImage //... emit updateImage(); } //... }; //... widgetThatPaintsImage->connect( workerThread, SIGNAL(updateImage()), SLOT(update()), Qt::QueuedConnection); //... //--------------postEvent Example----------------------- class WorkerThread : public QThread { //... protected: void run() { //construct image if(widgetThatPaintsImage) { QCoreApplication::postEvent( widgetThatPaintsImage, new QPaintEvent(widgetThatPaintsImage->rect())); } //... } private: QPointer<QWidget> widgetThatPaintsImage; };

不要忘记同步对图片的访问。 作为同步的替代方法,您还可以将图像发送到gui主题,例如 Mandelbrot示例。

Don't forget to synchronize the access to the image. As an alternative to the synchronization, you could also send the image to the gui thread, like in the Mandelbrot Example.

更多推荐

绘画QWidget外部GUI线程的问题

本文发布于:2023-11-24 04:35:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   QWidget   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!