PDA

View Full Version : can not paint picture in a widget which inside a qsrcollarea



hashb
18th August 2010, 11:19
Hi All,

I want to paint pic to a widget which inside a qscroll area,
but it seems no pic paint out

here is my code :



class Widget2 : public QWidget{
public:
Widget2(QWidget *parent) { }
protected:
void paintEvent(QPaintEvent*)
{
QPainter realPainter(this);
realPainter.drawPixmap(0,0,QPixmap("./01.png"));
}
};



class Widget : public QScrollArea{
public:
Widget(QWidget *parent=0)
{
w_ = new QWidget(this);
w_->resize(3000,3000);
setWidget(w_);
}
protected:
void paintEvent(QPaintEvent*)
{
QPainter realPainter(w_);
realPainter.drawPixmap(0,0,QPixmap("./01.png"));
}
private:
QWidget *w_;
};

int main(int argc ,char *argv[])
{
QApplication app(argc,argv);
Widget w;
w.show(); //the picture not paint out!!!
////////////////////////////
//the code blow works ,I was wondering why?:confused:
//Widget2 w;
//w.show(); //the picture not paint out!!!
/////////////////////////

app.exec();


}



Any ideas about it ?
Thanks advance for your help
Best regards,
hb

tbscope
18th August 2010, 11:35
Of course it doesn't work for your scrollarea.

I don't think you understand the paint event.
The paint event only paints when the widget gets a notification to repaint itself.
What you do is, paint another widget from the parent widget.

Although this logic might seem logical, it doesn't work.

Always paint widgets in their own paint event.
Otherwise, they might repaint themselves with the orignal paint code right after you've painted them from the parent (childs get repainted too when the parent gets repainted).

Why not add Widget2 to your scrollarea?

hashb
19th August 2010, 02:06
Hi tbscope,

Thanks a lot for your explanation :)

Best regards,
hb