Results 1 to 2 of 2

Thread: QGraphicsItem Mouse-handling

  1. #1
    Join Date
    Feb 2011
    Posts
    20
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsItem Mouse-handling

    In my subclassed QGraphicsItem I'd like to draw some points with my mouse. One click with the mouse will result in one point showing up at the screen.When I doubleclick the item will be added to the scene. Is this possible or must the item be added to the scene and then updated? Can anybody help me understand how I can get the mousepressed-event to my Item?

    MyScene.cpp
    Qt Code:
    1. void MyScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    2. {
    3. QGraphicsScene::mousePressEvent(mouseEvent);
    4. QPointF curPos= mouseEvent->lastScenePos();
    5.  
    6. //Create new item
    7. if(Mouse.iClicks == 0)
    8. {
    9. qDebug("Creating new spline.");
    10. newSpline = new itemSpline();
    11. }
    12.  
    13. newSpline->setVertices(curPos);
    14.  
    15. Mouse.iClicks++;
    16. }
    17.  
    18. void MyScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent)
    19. {
    20. //TODO: Remove last point because dblclick takes one position.
    21.  
    22. qDebug("MyScene: DoubleClickEvent");
    23. if(newSpline->size() > 1)
    24. {
    25. qDebug("Adding item");
    26. this->addItem(newSpline);
    27. this->addRect(newSpline->boundingRect());
    28. }
    29. Mouse.iClicks = 0;
    30. }
    To copy to clipboard, switch view to plain text mode 

    MyItem.cpp

    Qt Code:
    1. void item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setRenderHint(QPainter::Antialiasing);
    4. painter->setBrush(Qt::lightGray);
    5. //Draw line.
    6. }
    7.  
    8. void item::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
    9. {
    10. //Catch mouse pos.
    11. m.setText("Test");
    12. m.exec();
    13. }
    14.  
    15. void item::mousePressEvent(QGraphicsSceneMouseEvent* event)
    16. {
    17. m.setText("Test");
    18. m.exec();
    19. }
    20.  
    21. void item::mouseMoveEvent(QGraphicsSceneEvent *event)
    22. {
    23. //Draw line from start pos to current pos.
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2010
    Posts
    53
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem Mouse-handling

    Qt Code:
    1. #
    2. //Create new item
    3. #
    4. if(Mouse.iClicks == 0)
    5. #
    6. {
    7. #
    8. qDebug("Creating new spline.");
    9. #
    10. ----> newSpline = new itemSpline();
    11. #
    12. }
    To copy to clipboard, switch view to plain text mode 

    newSpline wasn't added to the scene, why do u belive scene should draw it?

    You need to understand what QGraphicsScene actually is. It is visualizer of the QGraphicsItems, that's mean u need thing u want to visualize to be QGraphicsItem and to be added to this scene.
    But since Qt is flexible, u always can reimplement QGraphicScene's paint() event. Good luck.

Similar Threads

  1. Handling mouse events in Qt WebKit
    By yakin in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2011, 05:44
  2. Handling mouse events
    By Maluko_Da_Tola in forum Newbie
    Replies: 13
    Last Post: 28th August 2010, 00:37
  3. Handling mouse click events in QGraphicsItem
    By Luc4 in forum Qt Programming
    Replies: 7
    Last Post: 5th March 2010, 16:12
  4. Mouse event handling beyond/outside the widget
    By keyur259 in forum Qt Programming
    Replies: 7
    Last Post: 20th January 2010, 05:14
  5. mouse events handling with QGraphicsItem
    By trallallero in forum Qt Programming
    Replies: 3
    Last Post: 21st October 2009, 14:15

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.