使用QT和QOGL的多线程崩溃 [英] Multithreading Crash using QT and QOGL

查看:453
本文介绍了使用QT和QOGL的多线程崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QOGLWidget小部件,构建在另一个窗口,它是通过按钮从QMainwindow激活。
我试图将一个图像的表面从窗口小部件发送到主窗口的QOGLWidget,它崩溃的问题,并给出第一次机会异常访问冲突。

I have an QOGLWidget widget which is constructed in a another window, which is activated by button from the QMainwindow. I'm trying to send a surface of an image from the widget to the main window's QOGLWidget, the problem that it crashes and gives a first chance exception access violation.

推荐答案

在这种情况下,我认为可能会有三件事情发生:

There are three things that I think may be going on in this situation:

您的框架生成器和GUI之间的连接(SimulatorWindow + GLSimulatorWidget)由 Qt :: AutoConnection ,最后变成一个 Qt :: DirectConnection 当它们驻留在单独的线程上。指定 Qt :: QueuedConnection 的连接以克服此问题

Your connection between your frame generator and the GUI (SimulatorWindow + GLSimulatorWidget) are connected by Qt::AutoConnection and ends up turning into a Qt::DirectConnection when they reside on separate threads. Specify a connection of Qt::QueuedConnection to overcome this problem (if indeed they are on separate threads).

您可以处理< a href =http://qt-project.org/doc/qt-5.0/qpixmap.html =nofollow> QPixmap 关闭GUI线程。这通常会打印出一个警告,但也许还是功能。一般来说,如果你处理一个图像不在GUI线程,然后发送到GUI线程,你需要使用除了 QPixmap ,例如 QImage

You could be handling QPixmaps off of the GUI thread. This usually will print out a warning, but maybe still function. In general if your processing an image not in the GUI thread, and then sending it to the GUI thread, you need to use something besides QPixmap, like QImage.

你的框架对象可能超出范围到你的GUI。例如,如果你构建一个框架,并将其存储在堆栈的本地范围,并将其传递给处理程序或容器...

Your frame object may be going out of scope by the time it gets to your GUI. For example, if you build a frame, and store it in a local scope on the stack, and you pass it into a handler or a container...

.. 。当你离开本地作用域以在GUI中表示对象时,你的框架对象将会消失,你的处理程序/容器留下一个坏的指针。

...then when you leave the local scope to representing the object in the GUI, your frame object will be gone and your handler/container is left holding a bad pointer.

解决此问题,初始化到堆上并坚持使用 Qt对象模型。另外,我喜欢把我的容器放在堆上,也作为它们属于的类的成员变量。

To fix this, initialize onto the heap and stick to the Qt Object Model. Also I like to make my containers on the heap also as member variables to the class they belong to.

所以总结:
检查你的连接类型,如果你的帧生成器是在GUI线程上或不是,使用QImage不QPixmap如果你在一个线程不是GUI,并确保你的指针不会超出范围。

So in summary: Check your connection types and if your frame generator is on the GUI thread or not, use QImage not QPixmap if you are in a thread that is not the GUI, and make sure your pointers are not going out of scope.

这篇关于使用QT和QOGL的多线程崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆