Results 1 to 3 of 3

Thread: paint with mouse

  1. #1
    Join Date
    Sep 2008
    Location
    Slovakia, Nitra
    Posts
    15
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question paint with mouse

    I want paint some circle by press the mouse button.
    Qt Code:
    1. void GraphArea::mousePressEvent1(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::LeftButton) {
    4. show = true;
    5. }
    6. }
    7.  
    8. void GraphArea::paintEvent(QPaintEvent * /*event*/)
    9. {
    10. QPainter painter(this);
    11. if (show)
    12. paintCircle1(painter);
    13. }
    To copy to clipboard, switch view to plain text mode 

    It works...
    ...but my plan/problem is: if i press mouse button 2nd time, then paint 2nd circle, to third press clear the whole widget - I don't know how to do it!

    PS.: sorry for my english...

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

    Default Re: paint with mouse

    Store data about the circles and access it during next mousepress.

    Qt Code:
    1. void xxx::mousePressEvent(QMouseEvent *me){
    2. // assuming QVector<QPair<QPoint, int> > circles is a member variable of the class
    3. switch(circles.count()){
    4. case 0: case 1: circles << qMakePair(me->pos(), 30); break;
    5. case 2: circles.clear();
    6. }
    7. update();
    8. }
    9. void xxx::paintEvent(QPaintEvent *pe){
    10. QPainter painter(this);
    11. for(int i=0;i<circles.count();i++){
    12. painter.drawEllipse(circles[i].first, circles[i].second, circles[i].second);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

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

    Vincenzo (19th October 2008)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: paint with mouse

    You have three states: no circles, one circle, two circles.

    So you need some variable that will hold the current state and then you have change this state accordingly in mousePressEvent() and paint the circles in paintEvent().

Similar Threads

  1. Mouse position on screen
    By Nippler in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2008, 14:22
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  4. Replies: 2
    Last Post: 24th July 2006, 18:36
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

Tags for this Thread

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.