Q_ASSERT failed in qt_win_display_dc()
2012-03-07 16:29
435 查看
在Qt中编程调试时有时会发生一个奇怪的错误,在qt_win_display_dc()函数中会发生Assert Failed,导致程序崩溃。该函数的代码如下:
Assert所判断的意思是当前运行的线程不是程序的主线程,也就是Qt中认为这段程序应该在主线程中执行才可以。最后知道是在其他的线程中要调用字体显示方面的函数,才导致这里的异常。
也有人说可以将这句Assert注释掉在编译Qt的源码来解决该问题,但是个人觉得既然源程序中这么写总是有原因的,简单的去掉这个Assert可能会造成其他的未知错误。所以还是从程序结构上来考虑如何避免该操作。
在Qt的文档中有这样的一些说明,摘录如下供参考。
In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations.
Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.
As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes,
for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.
Q_GUI_EXPORT HDC qt_win_display_dc() // get display DC { Q_ASSERT(qApp && qApp->thread() == QThread::currentThread()); if (!displayDC) displayDC = GetDC(0); return displayDC; }
Assert所判断的意思是当前运行的线程不是程序的主线程,也就是Qt中认为这段程序应该在主线程中执行才可以。最后知道是在其他的线程中要调用字体显示方面的函数,才导致这里的异常。
也有人说可以将这句Assert注释掉在编译Qt的源码来解决该问题,但是个人觉得既然源程序中这么写总是有原因的,简单的去掉这个Assert可能会造成其他的未知错误。所以还是从程序结构上来考虑如何避免该操作。
在Qt的文档中有这样的一些说明,摘录如下供参考。
In GUI applications, the main thread is also called the GUI thread because it's the only thread that is allowed to perform GUI-related operations.
Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.
As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes,
for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.
相关文章推荐
- This application failed to start because it could not find or load the Qt platform plugin "xcb" in "
- This application failed start because it could not find or load the Qt platform plugin "windows in"
- Mondrian Error:Internal error: assert failed: In the current implementation, parent/child hierarchies must have only one level (
- Failed to start program. Path or permissions wrong? in Qt
- IDA failed to display the program in graph mode
- This application failed to start because it could not find or load the Qt platform plugin "xcb" in "
- Debug: ..\..\src\common\image.cpp(1454): assert "Ok()" failed in wxImage::GetWidth(): invalid image
- QT28 how to display a splash screen in qt
- Qt_SDK_Lin32_offline_v1_1_3_en.run setup failed in ubuntu 12.04
- This application failed to start because it could not find or load the Qt platform plugin "xcb" in "
- This application failed to start because it could not find or load the Qt platform plugin "xcb" in "
- display:inline、block、inline-block的区别
- HTML5 display:inline、block、inline-block的区别--备用
- display:inline-block 不被IE7识别的解决方案
- QT5.1发布exe出错failed to load plaform plugin "windows"
- Failed to register in JMX: javax.naming.NamingException: Could not load resource factory class
- display:inline-block的div 与 display:block的div之间有间隔问题(div与div之间有间隔的可能性)
- centos下Svn检出Win2008 SVN服务器上项目SSL handshake failed: SSL error: Key usage violation in certificate ha
- How to Display DataSet in Silverlight DataGrid
- Failed to find target with hash string 'android-25' in:D:\SDK