最近在Qt项目中遇见一个问题比较奇怪,就在程序最小化之后,再点击显示发现界面会出现卡着不动的现象。
出现这个问题开始考虑是可能是样式导致的问题,但是让样式不起作用之后仍然出现该现象,在一番搜索之后发现有人已经解决该问题。
问题的原因:
widgets freezing after minimise window(点击图标,app不刷新,设置setWindowFlags(Qt::FramelessWindowHint)引起)
原因:What happened: Window stopped redrawing. It looked frozen.
I was debugging it and found out following:
After minimizing window attribute Qt::WA_Mapped
was removed from QMainWindow (you can set breakpoint to setAttribute_internal
in qwidget.cpp
to check it). But this attribute was not set again after showing window. This caused that condition if (discardSyncRequest(tlw, tlwExtra))
in QWidgetBackingStore::sync
was not met and it caused dirtyWidgets
are not cleared. In other part of Qt updating system this caused that no other rendering was made.
Workaround I did: Subclassed QMainWindow
and set attribute Qt::WA_Mapped
manually when window was restored (handling changeEvent):
void changeEvent(QEvent * event)
{
if(event->type() == QEvent::WindowStateChange)
{
if(!isMinimized())
{
setAttribute(Qt::WA_Mapped);
}
}
QWidget::changeEvent(event);
}
在此记录一下,以作以后遇见类似问题解决。