Results 1 to 3 of 3

Thread: Widget does not update

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Widget does not update

    I have probably (surely?) made a mistake somewhere but...impossible to find it out! Could you help please?

    Qt Code:
    1. class CircleParameter : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CircleParameter(QWidget *parent=0);
    7. virtual ~CircleParameter() {};
    8.  
    9. public slots:
    10. void setRadius(int radius);
    11.  
    12. protected:
    13. void paintEvent(QPaintEvent *e);
    14.  
    15. private:
    16. int radius;
    17. };
    18.  
    19. CircleParameter::CircleParameter(QWidget *parent)
    20. : QWidget(parent)
    21. {
    22. printf("creating widget\n");
    23. this->radius = DEFAULT_RADIUS;
    24. this->resize(MAX_RADIUS*2, MAX_RADIUS*2);
    25. if (updatesEnabled()) printf("yes\n"); else printf("no\n");
    26. if (isVisible()) printf("yes\n"); else printf("no\n");
    27. }
    28.  
    29. void
    30. CircleParameter::setRadius(int radius)
    31. {
    32. printf("setRadius()\n");
    33. if (updatesEnabled()) printf("yes\n"); else printf("no\n");
    34. if (isVisible()) printf("yes\n"); else printf("no\n");
    35. this->radius = radius>0 ? radius<=MAX_RADIUS ? radius : MAX_RADIUS : 1;
    36. this->update();
    37. }
    38.  
    39. void
    40. CircleParameter::paintEvent(QPaintEvent *e)
    41. {
    42. printf("paintEvent()\n");
    43. QPainter painter(this);
    44. QPen pen;
    45. pen.setColor(Qt::red);
    46. painter.setPen(pen);
    47. painter.drawEllipse(this->size().width()/2-this->radius, this->size().height()/2-this->radius, radius*2, radius*2);
    48. }
    To copy to clipboard, switch view to plain text mode 

    Outputs:
    - at creation time:
    Qt Code:
    1. creating widget
    2. yes
    3. no
    To copy to clipboard, switch view to plain text mode 
    - when moving a slider connected to CircleParameter::setRadius():
    Qt Code:
    1. setRadius()
    2. yes
    3. yes
    To copy to clipboard, switch view to plain text mode 

    Issue: why CircleParameter:: paintEvent() never executed?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Widget does not update

    If the CircleParameter widget is inside a layout, calling resize() will not work. You have to set a fixed size for your widget.

    The paint event probably doesn't get called because the widget size is (0, 0).
    Try printing this->size() in setRadius. It will most likely be (0, 0);

  3. #3
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Widget does not update

    Quote Originally Posted by marcel View Post
    If the CircleParameter widget is inside a layout, calling resize() will not work. You have to set a fixed size for your widget.
    I did not know about this behaviour of widgets in a layout. It works now. Thank you very much for your help!

Similar Threads

  1. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  2. Controlling which widget on top layer?
    By JonathanForQT4 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2007, 14:27
  3. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28
  4. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.