当前位置: 代码迷 >> QT开发 >> QPainter只能在paintEvent()中使用吗,该怎么解决
  详细解决方案

QPainter只能在paintEvent()中使用吗,该怎么解决

热度:70   发布时间:2016-04-25 04:55:16.0
QPainter只能在paintEvent()中使用吗
我在程序中定义了个Timer 每隔一段时间想更新一下 但是QPainter对象貌似只能在paintEvent中起作用 在Timer对应的slot中却无法使用 怎么也画不出来 是哪里出了问题呢?我的QPainter对象是私有成员 在paintEvent中初始化

------解决方案--------------------
首先,Manual 中有比较明确的说明:

The common use of QPainter is inside a widget's paint event: Construct and customize (e.g. set the pen or the brush) the painter. Then draw. Remember to destroy the QPainter object after drawing. 

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.

其次,在 paintEvent 中使用QPainter和你这儿的要求并不冲突。你在slot中update或repaint不就行了?