c++ - Qt paintEvent crashes -
im trying draw simple board on widget.
when i'm trying automatize this, paintevent crashes. think caused loop inside, right? how paint in other way?
void widget::paintevent(qpaintevent *event) { qpixmap mypix( qsize(20,20) ); qpainter painter(this); for(int = 0; < 100; i+5){ painter.drawline(qpointf(i,0),qpointf(i,max)); } this->setpixmap(mypix); }
your loop incorrect , causes program crash (i'm sure that's not fault here). should written this:
for(int = 0; < 100; i+=5){ p.drawline(qpointf(i,0),qpointf(i,max)); }
i.e. assignment of increment. way job , finish properly.
on side note, suggest use drawpixmap()
instead of setpixmap()
. setpixmap()
not cause infinite recursion , example next code works properly.
//... this->setpixmap(qpixmap("g:/2/qt.jpg")); qlabel::paintevent(event);
why? approach infinite recursion never produced (see here):
if call repaint() in function may called paintevent(), may infinite recursion. update() function never causes recursion.
indeed setpixmap()
calls update()
, not repaint()
. prove see source code:
void qlabel::setpixmap(const qpixmap &pixmap) { q_d(qlabel); //... d->updatelabel();//what does? }
void qlabelprivate::updatelabel() { q_q(qlabel); //... q->updategeometry(); q->update(q->contentsrect());//not repaint }
as said not mistake think better if need qpainter
.
Comments
Post a Comment