如何让子 widget 的 update 不引起父 widget 的 paintEvent

151 views
Skip to first unread message

Lanser

unread,
May 16, 2013, 6:27:54 AM5/16/13
to qt-c...@googlegroups.com

定义了 2 个 widget: parent, child. parent 是 child 的父,child 定时 update,看到总是 parent 的 paintEvent 先被调然后是 child 的 paintEvent。
但 child 其实是不透明、位置也是钉死的,不需要下面的东西先刷新,有没有方法?
--
Powered by interests.

Lanser

unread,
May 16, 2013, 9:57:20 PM5/16/13
to qt-c...@googlegroups.com
可以把 child 从 parent 的子关系中剥离,成为一个 top window,这样视觉效果是实现了,但不太好管理。


2013/5/16 Lanser <lans...@gmail.com>



--
Powered by interests.

Lanser

unread,
May 16, 2013, 10:57:51 PM5/16/13
to qt-c...@googlegroups.com
还可以把 parent 的 paintEvent 的 event->rect() 和 child  的 rect() 做对比。其他还有什么间便方法呢?


2013/5/17 Lanser <lans...@gmail.com>



--
Powered by interests.

vivian

unread,
May 16, 2013, 11:20:56 PM5/16/13
to qt-c...@googlegroups.com
不在child里定时update,改在parent里定时update,然后在parent里的paintEvent使用QPainter(child)之类的办法,不知道这样是否管用
我没试过,只是设想一下


--
您收到此邮件是因为您订阅了 Google 网上论坛的“Qt中文”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 qt-china+u...@googlegroups.com
要向此网上论坛发帖,请发送电子邮件至 qt-c...@googlegroups.com
通过以下网址访问此论坛:http://groups.google.com/group/qt-china?hl=zh-CN。
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 



--
不抛弃 不放弃
www.bilive.com
做优秀的软件

Jian Yang

unread,
May 17, 2013, 1:30:41 AM5/17/13
to qt-c...@googlegroups.com
这和Qt的绘图机制有关系,Qt 里有backingstore的机制,Qt 4.4开始有了Alien Widget,你的子Widget并不对应一个真正的HWND,它的父窗口,或者父窗口的父窗口才对应一个HWND,这样你更新子Widget,实际上必须更新父窗口。

如果你确实只想更新子窗口,可以使用setAttribute(Qt::WA_PaintOnScreen); 把widget指定成Native Widget。
但是WA_PaintOnScreen只支持X11,在Windows下,你需要重载 paintEngine() const 返回NULL。然后在paintEvent中,使用GDI来绘图到HDC上,不能再使用QPainter。

Jian Yang

unread,
May 17, 2013, 1:31:29 AM5/17/13
to qt-c...@googlegroups.com
这和Qt的绘图机制有关系,Qt 里有backingstore的机制,Qt 4.4开始有了Alien Widget,你的子Widget并不对应一个真正的HWND,它的父窗口,或者父窗口的父窗口才对应一个HWND,这样你更新子Widget,实际上必须更新父窗口。

如果你确实只想更新子窗口,可以使用setAttribute(Qt::WA_PaintOnScreen); 把widget指定成Native Widget。
但是WA_PaintOnScreen只支持X11,在Windows下,你需要重载 paintEngine() const 返回NULL。然后在paintEvent中,使用GDI来绘图到HDC上,不能再使用QPainter。


在 2013年5月17日上午11:20,vivian <icevi...@gmail.com>写道:

Lanser

unread,
May 17, 2013, 2:30:09 AM5/17/13
to qt-c...@googlegroups.com
不能这样做,QPainter::begin: Widget painting can only begin as a result of a paintEvent
不能在本 widget 的 paintEvent 里初始化其他 widget 的 painter


2013/5/17 vivian <icevi...@gmail.com>



--
Powered by interests.

Lanser

unread,
May 17, 2013, 2:42:21 AM5/17/13
to qt-c...@googlegroups.com
可惜我是工作在 qws 上,没法直接 WA_PaintOnScreen。不过 WA_OpaquePaintEvnt 倒是满足要求


Indicates that the widget paints all its pixels when it receives a paint event. Thus, it is not required for operations like updating, resizing, scrolling and focus changes to erase the widget before generating paint events. The use of WA_OpaquePaintEvent provides a small optimization by helping to reduce flicker on systems that do not support double buffering and avoiding computational cycles necessary to erase the background prior to painting. Note: Unlike WA_NoSystemBackground, WA_OpaquePaintEvent makes an effort to avoid transparent window backgrounds. This flag is set or cleared by the widget's author.


2013/5/17 Jian Yang <marin...@gmail.com>



--
Powered by interests.
Reply all
Reply to author
Forward
0 new messages