简体   繁体   English

用QPainter绘制

[英]Draw with QPainter

I need draw with QPainter , but when I try to do it not in 我需要使用QPainter绘制,但是当我尝试这样做时,

void paintEvent( QPaintEvent* )

I catch error: 我发现错误:

QPainter::setPen: Painter not active QPainter :: setPen:画家不活跃

QPainter::setBrush: Painter not active QPainter :: setBrush:画家不活跃

Can I use QPainter not in void paintEvent( QPaintEvent* ) ? 我可以在无效的paintEvent( QPaintEvent* )使用QPainter吗?

Thank you. 谢谢。

You are using a widget as the paint device for QPainter . 您正在使用小部件作为QPainter的绘制设备。 In this case, QPainter can usually only be used within the context of the widget's paint event. 在这种情况下, QPainter通常只能在小部件的paint事件的上下文中使用。 The QPainter documentation has the following to say about this: QPainter文档对此有以下说法:

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); 警告:当paintdevice是窗口小部件时,QPainter只能在paintEvent()函数内部或paintEvent()调用的函数中使用; that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. 除非设置了Qt :: WA_PaintOutsidePaintEvent小部件属性。 On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting. 在Mac OS X和Windows上,无论此属性的设置如何,都只能在paintEvent()函数中进行绘制。

It is possible to use QPainter outside a paint event by setting another paint device for QPainter , for example a QPixmap . 通过为QPainter设置另一个绘制设备(例如QPixmap ,可以在绘制事件之外使用QPainter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM