PDA

View Full Version : Problem with QListWidget and paintEvent()



joksi
27th May 2009, 14:44
I have inherited QListWidget and rewrote the paintEvent()-function as follows:


void InheritedListWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);

static const QPointF points[3] = {
QPointF(2,2),
QPointF(this->width()-2,2),
QPointF(2,this->height()-2)
};
painter.setBrush(Qt::black);
painter.drawPolygon(points, 3);
}

Now, for some reason when I'm running the program, nothing is painted on the list widget and the application prints to console:


QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::setBrush: Painter not active

What is wrong with my code?

birchbay
29th May 2009, 10:18
You should give viewport to QPainter.



QPainter painter(this->viewport());


See Qt docs:



void QAbstractScrollArea:: paintEvent ( QPaintEvent * event ) [virtual protected]

This event handler can be reimplemented in a subclass to receive paint events (passed in event), for the viewport() widget.

Note: If you open a painter, make sure to open it on the viewport().

joksi
29th May 2009, 10:50
Thanks birchbay, this solved my problem!