Results 1 to 4 of 4

Thread: drawing points based on mouse clicks, how to create handle event for mouse clicks

  1. #1
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default drawing points based on mouse clicks, how to create handle event for mouse clicks

    hi, I have a small problem in knowing the position of a mouse on a QGraphicsViewScene. How can i know the x,y and handle clicks to draw points in their place and later create a shape filled with black color.
    regards

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: drawing points based on mouse clicks, how to create handle event for mouse clicks

    You use the mouse events of the scene.
    http://doc.qt.nokia.com/4.6/qgraphic...mouseMoveEvent

  3. #3
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: drawing points based on mouse clicks, how to create handle event for mouse clicks

    thanks, i was alble to use mousePressEvent and was able to update the cooirdinates in a lable but i couldn't use QGraphicsSceneMouseEvent, i changed (QMouseEvent to QGraphicsSceneMouseEvent) but it didnt return anything. the problem is that the cooirdinates are relative to the main window not my GraphicsView. please see attached image for demonstration.

    Screen shot 2010-&#4.jpg

    Qt Code:
    1. void MainWindow::mousePressEvent(QMouseEvent *mouseEvent)
    2.  
    3. {
    4.  
    5. QVariant x(mouseEvent->x());
    6. QVariant y(mouseEvent->y());
    7. ui->lblX->setText(x.toString());
    8. ui->lblY->setText(y.toString());
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2010
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: drawing points based on mouse clicks, how to create handle event for mouse clicks

    for any one who reads this i did it using QEventFilter.
    this topic was helpful: http://www.qtcentre.org/threads/6272...cking-on-image

    this is the code i use:
    in header file:
    Qt Code:
    1. bool eventFilter(QObject *o, QEvent *e);
    To copy to clipboard, switch view to plain text mode 

    in .cpp

    Qt Code:
    1. //filter mouse tracking to the selection area only
    2. bool MainWindow::eventFilter(QObject *o, QEvent *e){
    3. if(o!= lblScrollImage) return false;
    4. if(e->type()==QEvent::MouseMove){
    5. QMouseEvent *mev = static_cast<QMouseEvent*>(e);
    6. QVariant x(mev->pos().x());
    7. QVariant y(mev->pos().y());
    8. ui->lblX->setText(x.toString());
    9. ui->lblY->setText(y.toString());
    10. //polyPoints << QPoint(x.toInt(), y.toInt());
    11. }
    12. else if (e->type()==QEvent::MouseButtonPress)
    13. {
    14. QMouseEvent *mev = static_cast<QMouseEvent*>(e);
    15. QVariant x(mev->pos().x());
    16. QVariant y(mev->pos().y());
    17. ui->lblX->setText(x.toString());
    18. ui->lblY->setText(y.toString());
    19. ui->lineEditX->setText(x.toString());
    20. ui->lineEditY->setText(y.toString());
    21. polyPoints << QPoint(x.toInt(), y.toInt());
    22. }
    23. return false;
    24. }
    To copy to clipboard, switch view to plain text mode 


    regards,
    ahmed

Similar Threads

  1. Generating Mouse Clicks
    By Ebonair in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2009, 18:20
  2. How to handle double clicks with mouse?
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 14th January 2007, 12:01
  3. traversing tree using mouse clicks
    By krishna.bv in forum Qt Programming
    Replies: 3
    Last Post: 22nd December 2006, 09:15
  4. Replies: 3
    Last Post: 29th August 2006, 15:07
  5. Detecting mouse clicks outside of widget
    By init2null in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2006, 19:16

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.