Results 1 to 5 of 5

Thread: how to draw points with qpainter?

  1. #1
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default how to draw points with qpainter?

    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent *f)
    2. {
    3. point=f->pos();
    4. y=1;
    5. update();
    6. }
    7.  
    8. void MainWindow::paintEvent(QPaintEvent *e)
    9. {
    10. QPainter painter(this);
    11. QPen linepen(Qt::red);
    12. linepen.setCapStyle(Qt::RoundCap);
    13. linepen.setWidth(30);
    14. painter.setRenderHint(QPainter::Antialiasing,true);
    15. painter.setPen(linepen);
    16. if(y==1)
    17. painter.drawPoint(point);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    i have used the code. but draws only single point. when i click to draw a second point the first point will disappear. at a time only one point will appear..

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to draw points with qpainter?

    Its because paintEvent clears the widget before painting, so everything previously painted is gone.
    You should try using a QList of points to store every clicked point and paint all of them.

  3. #3
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: how to draw points with qpainter?

    k i will try to do it. thanks


    Added after 29 minutes:


    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent *e)
    2. {
    3. point=e->pos();
    4. update();
    5. }
    6. void MainWindow::paintEvent(QPaintEvent *e)
    7. {
    8. setAttribute(Qt::WA_OpaquePaintEvent);
    9. QPainter painter(this);
    10. QPen linepen(Qt::red);
    11. linepen.setCapStyle(Qt::RoundCap);
    12. linepen.setWidth(30);
    13. painter.setRenderHint(QPainter::Antialiasing,true);
    14. painter.setPen(linepen);
    15. painter.drawPoint(point);
    16. }
    To copy to clipboard, switch view to plain text mode 

    it worked for no need for list
    Last edited by athulms; 19th August 2011 at 10:35.

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

    embeddedmz (22nd August 2022)

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to draw points with qpainter?

    it worked for no need for list
    That is only because you are only using your dedicated painting in your paint event.
    But what if you want the standard painting AND your painting?
    Or if you cover your widget with another window, and then show your widget again?
    Then you will only get the last point.
    What stampede suggested is the correct way.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #5
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: how to draw points with qpainter?

    k thanks i will do it with list

Similar Threads

  1. How to draw arc between two points.
    By nikhil in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2011, 13:55
  2. Clear previous points when using draw(from,to)
    By huseyinkozan in forum Qwt
    Replies: 4
    Last Post: 1st April 2009, 23:11
  3. want to draw points in QGraphicsScene
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2008, 18:14
  4. How to draw some points and lines?
    By luffy27 in forum Qt Programming
    Replies: 1
    Last Post: 24th November 2006, 16:47

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.