[Qt-interest] Problem with QPainter

Michael user23 at web.de
Sat Jul 24 15:23:07 CEST 2010


Am 24.07.2010 12:55, schrieb Vijay Kansal:
> Hi,
> I was just beginning to use QPainter and this my sorce code :
>  
>         QLine line( 0, 10, 10, 10 );
>     QPen pen( Qt::blue, 10 ); 
>     QPainter painter;
>     painter.begin(wave->device));
>     painter.drawLine(line);
>
> wave->device is a pointer to a QPushButton object.
> painter.begin is returning false and at the application output i could
> see the statment
>  
>       QPainter::begin: Paint device returned engine == 0, type: 1
>  
> I am unable to make out why my painter object is unable to begin
> Is there anything wrong with my source code.
> Please help.
> I am using Windows XP .
>
> -- 
> vijay

Hello vijay,

In the documentation regarding QPainter there is a warning:

*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
<qt.html#WidgetAttribute-enum> widget attribute is set. On Mac OS X and
Windows, *you can only paint in a paintEvent()* function regardless of
this attribute's setting.

So what you have to do is to reimplement QPushButton and overwrite the paint function like this:


class MyButton: public QPushButton {
Q_OBJECT
....
protected:
virtual void paintEvent ( QPaintEvent * event ) {
QWidget::paintEvent(event);

    QPen pen( Qt::blue, 10 ); 

    QPainter painter(this);

    painter.drawLine(QLine(0,10,10,10));
}

};
Note: this is quickly written and may contain typos.

Hope this helps
Michael


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100724/88d73cb3/attachment.html 


More information about the Qt-interest-old mailing list