Results 1 to 3 of 3

Thread: can not paint picture in a widget which inside a qsrcollarea

  1. #1
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Lightbulb can not paint picture in a widget which inside a qsrcollarea

    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 :
    Qt Code:
    1. class Widget2 : public QWidget{
    2. public:
    3. Widget2(QWidget *parent) { }
    4. protected:
    5. void paintEvent(QPaintEvent*)
    6. {
    7. QPainter realPainter(this);
    8. realPainter.drawPixmap(0,0,QPixmap("./01.png"));
    9. }
    10. };
    11.  
    12.  
    13.  
    14. class Widget : public QScrollArea{
    15. public:
    16. Widget(QWidget *parent=0)
    17. {
    18. w_ = new QWidget(this);
    19. w_->resize(3000,3000);
    20. setWidget(w_);
    21. }
    22. protected:
    23. void paintEvent(QPaintEvent*)
    24. {
    25. QPainter realPainter(w_);
    26. realPainter.drawPixmap(0,0,QPixmap("./01.png"));
    27. }
    28. private:
    29. QWidget *w_;
    30. };
    31.  
    32. int main(int argc ,char *argv[])
    33. {
    34. QApplication app(argc,argv);
    35. Widget w;
    36. w.show(); //the picture not paint out!!!
    37. ////////////////////////////
    38. //the code blow works ,I was wondering why?:confused:
    39. //Widget2 w;
    40. //w.show(); //the picture not paint out!!!
    41. /////////////////////////
    42.  
    43. app.exec();
    44.  
    45.  
    46. }
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: can not paint picture in a widget which inside a qsrcollarea

    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?

  3. The following user says thank you to tbscope for this useful post:

    hashb (19th August 2010)

  4. #3
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: can not paint picture in a widget which inside a qsrcollarea

    Hi tbscope,

    Thanks a lot for your explanation

    Best regards,
    hb

Similar Threads

  1. Replies: 1
    Last Post: 8th January 2010, 20:03
  2. Widget Overlapping picture
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2009, 14:50
  3. Copy full widget as a picture to clipboard
    By lasher in forum Newbie
    Replies: 1
    Last Post: 26th September 2009, 08:25
  4. Replies: 33
    Last Post: 8th July 2008, 07:23
  5. How to paint a widget outside paintEvent()
    By wesley in forum Qt Programming
    Replies: 10
    Last Post: 27th February 2008, 03:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.