Results 1 to 4 of 4

Thread: Repainting widget

  1. #1
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question Repainting widget

    My widget doesn't repaint correctly. I have no idea why.

    Step 1:


    Step 2:


    Step 3:


    Qt Code:
    1. [...]
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MainWindow();
    9. MainWindow(QWidget * parent, const char * name);
    10. ~MainWindow();
    11.  
    12. public slots:
    13. void clearFill();
    14. void clear();
    15. void reset();
    16.  
    17. protected:
    18. void paintEvent(QPaintEvent * e);
    19. void mouseMoveEvent(QMouseEvent * e);
    20. void mousePressEvent(QMouseEvent * e);
    21. void mouseDoubleClickEvent(QMouseEvent * e);
    22.  
    23. private:
    24. Painter * painter;
    25.  
    26. [...]
    27.  
    28. void MainWindow::paintEvent(QPaintEvent * e)
    29. {
    30. painter->repaint();
    31.  
    32. QMainWindow::paintEvent(e);
    33. }
    34.  
    35. [...]
    36.  
    37. class Painter
    38. {
    39. public:
    40. [...]
    41.  
    42. private:
    43. [...]
    44. QPainter * painter;
    45.  
    46. [...]
    47.  
    48. void Painter::repaint()
    49. {
    50. for (int i = 0 ; i != width ; i++)
    51. {
    52. for (int j = 0 ; j != height ; j++)
    53. drawPoint(array[i][j]);
    54. }
    55.  
    56. for (SimpleSegmentList::iterator i = list.begin() ; i != list.end() ; i++)
    57. drawLine((*i));
    58. }
    59.  
    60. [...]
    61.  
    62. void Painter::drawPoint(const Point & p)
    63. {
    64. if (!painter)
    65. return;
    66.  
    67. painter->setPen(QPen(QColor(0, 0, 0)));
    68. painter->setBrush(QBrush(p.color()));
    69. painter->drawEllipse(p.center().x(), p.center().y(), p.dia(), p.dia());
    70. }
    71.  
    72. void Painter::drawLine(const SimpleSegment & s)
    73. {
    74. if (!painter)
    75. return;
    76.  
    77. const int hdia = array[0][0].dia() / 2;
    78.  
    79. QPoint start = point(s.start()).center();
    80. QPoint stop = point(s.stop()).center();
    81.  
    82. start.setX(start.x() + hdia);
    83. start.setY(start.y() + hdia);
    84. stop.setX(stop.x() + hdia);
    85. stop.setY(stop.y() + hdia);
    86.  
    87. painter->setPen(QPen(s.color(), hdia));
    88. painter->drawLine(start, stop);
    89. }
    90.  
    91. [...]
    To copy to clipboard, switch view to plain text mode 
    Last edited by fear; 25th March 2008 at 19:33.

  2. #2
    Join Date
    Feb 2008
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Repainting widget

    No idea, but here's a few brainfarts from when I read your sample code:

    - Is your Painter a subclass from QPainter?
    - Try to rename your repaint method to something else..

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Repainting widget

    You have to create a new painter during each paint event and I don't see that in your code.

  4. The following user says thank you to wysota for this useful post:

    fear (5th May 2008)

  5. #4
    Join Date
    Mar 2007
    Posts
    31
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Repainting widget

    Thank You. That's true. In fact api doc doesn't say it in explicit way. I was always using QPainter in this way:

    Mostly, all this is done inside a paint event. (In fact, 99% of all QPainter use is in a reimplementation of QWidget::paintEvent(), and the painter is heavily optimized for such use.) Here's one very simple example:
    Qt Code:
    1. void SimpleExampleWidget::paintEvent()
    2. {
    3. QPainter paint( this );
    4. paint.setPen( Qt::blue );
    5. paint.drawText( rect(), AlignCenter, "The Text" );
    6. }
    To copy to clipboard, switch view to plain text mode 
    So now I realized that it MUST be used in such way.

Similar Threads

  1. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  2. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 14:06
  3. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  4. Controlling which widget on top layer?
    By JonathanForQT4 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2007, 14:27
  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.