Results 1 to 5 of 5

Thread: QPainter - drawing new graphics without removing old stuff

  1. #1
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QPainter - drawing new graphics without removing old stuff

    Folks,

    A trivial question. How do I draw stuff on the canvas without erasing the old stuff?

    Consider this code snippet for drawing 200 random points on a 320x240 window:

    void dlg::timerEvent(QTimerEvent *event)
    {
    if (count < 200) {
    count=count+1;
    x = rand() % 320;
    y = rand() % 240;
    update();
    } else {
    timer.stop();
    }
    }

    void dlg::paintEvent(QPaintEvent *)
    {
    QPainter painter(this);
    painter.drawPoint(x,y);
    update();
    }

    It'd draw one point, erase the screen and then draw the next point. I don't want that. I want the old points to stay. How do I do this?

    Thanks,

    Ricky.

  2. #2
    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: QPainter - drawing new graphics without removing old stuff

    Keep a list of points and draw them all.

  3. #3
    Join Date
    Mar 2008
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter - drawing new graphics without removing old stuff

    I want a delay of 0.5 seconds before drawing each point, hence the timer. Not sure how this'd work out if I had the list of all 200 points in an array... code snippet anyone?

    Quote Originally Posted by wysota View Post
    Keep a list of points and draw them all.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter - drawing new graphics without removing old stuff

    something like -
    Qt Code:
    1. QTimer::singleShot(500,this, onTimer());
    2.  
    3. void Dlg::OnTimer()
    4. {
    5. m_index_of_point++;
    6. drawPoint(m_pointList[m_index_of_point],x,y); // move the index to next point and draw it at x,y positiom.
    7. }
    To copy to clipboard, switch view to plain text mode 

    Hope u get the idea

  5. The following user says thank you to aamer4yu for this useful post:

    rickysts (14th April 2008)

  6. #5
    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: QPainter - drawing new graphics without removing old stuff

    Quote Originally Posted by rickysts View Post
    I want a delay of 0.5 seconds before drawing each point, hence the timer. Not sure how this'd work out if I had the list of all 200 points in an array... code snippet anyone?
    Qt Code:
    1. void X::timerEvent(QTimerEvent *te){
    2. QPoint pt(qRand()%width(), qRand()%height());
    3. points << pt; // assuming "points" is a QList<QPoint> class member
    4. update();
    5. }
    6.  
    7. void X::paintEvent(QPaintEvent *pe){
    8. QPainter p(this);
    9. QPen pe = p.pen();
    10. pe.setWidth(2);
    11. p.setPen(pe);
    12. foreach(const QPoint &pt, points){
    13. p.drawPoint(pt);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    rickysts (14th April 2008)

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.