PDA

View Full Version : How to manage QPainter?



Caius Aérobus
26th April 2006, 11:00
Hello,
all examples I have found up to now create the painter each time they need it but I would like to be able to design a general drawing class, in which a painter is defined with a numpber of common attributes, and then design subclasses using this commonly defined painter. So I have the general class creator definied as:

vadVisualization::vadVisualization(QAbstractItemMo del *model, vad *app, QWidget *parent,
int xsize, int ysize, char *name, int debug)
: QAbstractItemView(parent)
{
this->setModel(model);
[...]
this->painter = new QPainter(this->viewport());
}

and the specific class paintEvent method defined as:

void
vadControlPolygon::paintEvent(QPaintEvent *event)
{
QAbstractItemView::paintEvent(event);
setDirtyRegion(this->viewport()->rect());
this->painter->begin(this->viewport());
this->painter->setWindow(0, 0, 100, 100);
[...]
this->painter->drawPolygon(this->polygon);
// les sommets
for (int i=0 ; i<3 ; i++)
this->itemDelegate()->paint(this->painter, option, this->model()->index(i, 0));
this->painter->end();
}

but I get the following errors:

QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::begin(): Painter is already active.
You must end() the painter before a second begin()
QPainter::setWindow(), painter not active
QPainter::end: Painter is not active, aborted
QPainter::begin(): Painter is already active.
You must end() the painter before a second begin()
QPainter::setWindow(), painter not active
QPainter::end: Painter is not active, aborted
QPainter::begin(): Painter is already active.

so ok I understand that my painter is already active, which means that I do not need to begin() and end() it, and then I change my code by withdrawing the begin() and end() calls. Then I get the following errors:

QPainter::begin: Widget painting can only begin as a result of a paintEvent
QPainter::setWindow(), painter not active
QPainter::setWindow(), painter not active
QPainter::setWindow(), painter not active
QPainter::setWindow(), painter not active
QPainter::setWindow(), painter not active
QPainter::setWindow(), painter not active

so please coulod anybody explains me how painter are managed by Qt and how to cope with this in my case?

evgenM
26th April 2006, 13:46
don't use painter->begin() and painter->end()
u need only
QPainter painter( viewport() );

jacek
26th April 2006, 15:40
Or try:
painter = new QPainter( 0 );
...
painter->begin( this );
...
painter->end();

Caius Aérobus
28th April 2006, 14:20
EvgenM: as I wrote in my first message, I already tested this solution and it does not work!
The right solution is those suggested by Jacek, that is I have to create the painter without attaching it to any devide, and then attach is with begin() each time I need it.
Thanks for your help Jecek.