Results 1 to 4 of 4

Thread: How can we call the QPainter under QMouseEvent

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default How can we call the QPainter under QMouseEvent

    hi all,

    I have a problem. I need to draw circles when I click the mainwindow.Therefore the code of the drawing circles should be under the mouseevent function, isn't it? However, I couldn't do it. It give error says "Painter not active". Can anybody help on this issue?

  2. #2
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can we call the QPainter under QMouseEvent

    Quote Originally Posted by cemshit View Post
    I need to draw circles when I click the mainwindow.Therefore the code of the drawing circles should be under the mouseevent function, isn't it?
    Code for drawing virtually always goes in QWidget::paintEvent.

    In working with GUI systems, remember that anything drawn can need to be redrawn at any time — say, because a window from another application comes to the front, overwrites your window, and is then removed. So it’s not like you’ll be drawing this circle just once, when the mouse clicks; you must be prepared to draw it upon request (that is, whenever QWidget::paintEvent is called) for as long as the circle should be displayed.

    You will need some data structure within your mainwindow that maintains the information needed to draw the circle (or circles, if existing ones are to stay visible as new ones are added). That information is what you update in QWidget::mousePressEvent or QWidget::mouseReleaseEvent, after which you call QWidget::update to tell Qt to repaint the window. In QWidget::paintEvent you draw the circle(s) indicated in your data structure, regardless of how QWidget::paintEvent came to be called.

  3. #3
    Join Date
    Jan 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can we call the QPainter under QMouseEvent

    Qt Code:
    1. Point clickedPoint;
    2.  
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. setupUi(this);
    8. }
    9.  
    10. MainWindow::~MainWindow()
    11. {
    12. }
    13.  
    14.  
    15. void MainWindow::mousePressEvent( QMouseEvent *event)
    16. {
    17.  
    18. if( frame1->underMouse() && event->x() > 60){
    19. qDebug() << "global Pos: " << event->globalPos();
    20. qDebug() << "frame glob: " << frame1->mapFromGlobal( event->globalPos() );
    21.  
    22. clickedPoint=event->pos();
    23. QWidget::mousePressEvent(event);
    24.  
    25. }
    26. QMainWindow::mousePressEvent( event );
    27. }
    28.  
    29. void MainWindow::paintEvent(QPaintEvent *event)
    30. {
    31. QPainter painter(this);
    32.  
    33. painter.setRenderHint(QPainter::Antialiasing);
    34.  
    35.  
    36. painter.setBrush(Qt::white);
    37. //painter.setPen(QPen(QBrush("#575555"), 1));
    38. painter.drawRect(10, 10, 680, 500);
    39.  
    40. QWidget::paintEvent(event);
    41.  
    42. }
    To copy to clipboard, switch view to plain text mode 


    first of all thanks for your respond in the forum. My code is above. What sould I add the code to draw circles where I press every time. As you say I want to keep previous circles and add new ones with clicking where I want to put them.

  4. #4
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can we call the QPainter under QMouseEvent

    Add to your definition of class MainWindow (probably in the mainwindow.h file) a data structure to hold the points at which you have clicked. I suggest using a QList<QPoint> for that. In mousePressEvent, add the point where you clicked to the list and call update(). Then, in paintEvent, iterate through your list drawing each circle.

Similar Threads

  1. QMouseEvent
    By shenakan in forum Newbie
    Replies: 1
    Last Post: 20th August 2009, 14:53
  2. QMouseEvent with two mice
    By yun in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2009, 13:42
  3. help needed in QMouseEvent
    By aj2903 in forum Qt Programming
    Replies: 21
    Last Post: 14th February 2009, 11:15
  4. qmouseEvent handling
    By nass in forum Qt Tools
    Replies: 9
    Last Post: 13th October 2006, 08:55
  5. QToolBox and QMouseEvent
    By mickey in forum Qt Programming
    Replies: 6
    Last Post: 1st February 2006, 11:57

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.