Results 1 to 3 of 3

Thread: Painting the mouse trace in a QGraphicsItem

  1. #1
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Painting the mouse trace in a QGraphicsItem

    Hello,

    I have a QGraphicsView with a scene that displays an image. What I am trying to do now is when I click the mouse and move without un-clicking, I want to draw the mouse trace until the mouse button is released. So basically to draw with the mouse.

    Could someone please guide me to what I need to do for this?

    So far what I am trying to do is subclassing QGraphicsRectItem and from the mouse events of the QGV call my own functions in the GraphicsItem subclass passing as a parameter the QMouseEvent and handling things there.

    I know I have to implement the paint event to draw, but I’m a bit lost how to approach this. I was thinking maybe getting the points of the trace and drawing small line segments in between, would this be a good possible way?

    I appreciate any help. Thanks.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Painting the mouse trace in a QGraphicsItem

    There's many ways of doing what you want.
    Subclassing is one of them.

    Take a look at this example, here I use event filter to do what you want (compile and try it out):
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QGraphicsScene>
    4. #include <QGraphicsView>
    5. #include <QGraphicsPathItem>
    6. #include <QEvent>
    7. #include <QGraphicsSceneMouseEvent>
    8.  
    9. MainWindow::MainWindow(QWidget *parent)
    10. :
    11. QMainWindow( parent ),
    12. scene( new QGraphicsScene( this ) ),
    13. view( new QGraphicsView( this->scene, this ) ),
    14. item( NULL )
    15. {
    16. this->scene->setSceneRect( 0, 0, 1000, 1000 );
    17.  
    18. this->setCentralWidget( this->view );
    19.  
    20. this->scene->installEventFilter( this );
    21. }
    22.  
    23. MainWindow::~MainWindow()
    24. {
    25.  
    26. }
    27.  
    28. bool MainWindow::eventFilter( QObject* o , QEvent* e )
    29. {
    30. switch( e->type() )
    31. {
    32. case QEvent::GraphicsSceneMousePress:
    33. {
    34. QGraphicsSceneMouseEvent* event = static_cast< QGraphicsSceneMouseEvent* >( e );
    35.  
    36. pp.moveTo( event->scenePos() );
    37.  
    38. this->item = new QGraphicsPathItem();
    39. this->item->setPath( pp );
    40. this->scene->addItem( this->item );
    41. break;
    42. }
    43. case QEvent::GraphicsSceneMouseMove:
    44. {
    45. if( this->item )
    46. {
    47. QGraphicsSceneMouseEvent* event = static_cast< QGraphicsSceneMouseEvent* >( e );
    48.  
    49. QPainterPath pp = this->item->path();
    50. pp.lineTo( event->scenePos() );
    51. this->item->setPath( pp );
    52. }
    53. break;
    54. }
    55. case QEvent::GraphicsSceneMouseRelease:
    56. {
    57. this->item = NULL;
    58. break;
    59. }
    60. default:
    61. break;
    62. }
    63.  
    64. return false;
    65. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Painting the mouse trace in a QGraphicsItem

    Can this be done without mousepress event.

    I want to draw where ever the mouse cursor moves (without clicking).!!!

Similar Threads

  1. Painting problem for QGraphicsItem
    By vivekpurohit in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2011, 10:55
  2. specific painting for just one QGraphicsItem
    By jano_alex_es in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2011, 02:12
  3. QGraphicsItem - painting problem for zoomin
    By nileshsince1980 in forum Qt Programming
    Replies: 0
    Last Post: 26th March 2010, 10:02
  4. [SOLVED] Painting QGraphicsItem constant size
    By JovianGhost in forum Qt Programming
    Replies: 7
    Last Post: 22nd March 2010, 02:25
  5. Force the painting of a QGraphicsItem
    By fabietto in forum Qt Programming
    Replies: 3
    Last Post: 2nd July 2007, 22:28

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.