PDA

View Full Version : Subclassing QWidget not working



guidupas
26th May 2014, 15:49
Hello all!

I need to subclass QWidget, but its not working. When I call the function desenhaFluxo(), it returns the error:
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not active

How can I do that?



#include "fluxocaixawidget.h"

fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
QWidget(parent)
{

}

void fluxoCaixaWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);

QPen caneta(Qt::black);
caneta.setWidth(4);
painter.setPen(caneta);



painter.drawLine(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-40, this->parentWidget()->height() / 2 - 20);
}

void fluxoCaixaWidget::desenhaFluxo()
{
QPainter painter(this);

QPen caneta(Qt::black);
caneta.setWidth(4);
painter.setPen(caneta);

painter.drawLine(10,10,50,10);
}

anda_skoa
26th May 2014, 18:48
You cannot paint on a widget outside its paintEvent() method,

Either store the line parameters in your class and use the in paintEvent() and trigger an update by calling update() in desenhaFluxo()
or
Paint into a QPixmap and let paintEvent() only paint that pixmap.

Cheers,
_