Im trying to draw simple board on my widget.
When I'm trying to automatize this, my paintEvent crashes. I think it is caused by for loop inside, am I right? How to paint it in other way?
void Widget::paintEvent(QPaintEvent *event)
{
QPixmap myPix( QSize(20,20) );
QPainter painter(this);
for(int i = 0; i < 100; i+5){
painter.drawLine(QPointF(i,0),QPointF(i,max));
}
this->setPixmap(myPix);
}
Your for loop is incorrect and causes the program crash (I'm sure that's not your fault here). It should be written like this:
i.e. with an assignment of the increment. This way it will do the job and finish properly.
On a side note, I would suggest to use
drawPixmap()
instead ofsetPixmap()
. ButsetPixmap()
will not cause infinite recursion and for example next code works properly.Why? With this approach infinite recursion is never produced (see here):
Indeed
setPixmap()
callsupdate()
, notrepaint()
. To prove that see source code:setPixmap source:
updateLabel source:
As I said it is not a mistake but I think that it will be better if you will do all what you need with
QPainter
.