Results 1 to 2 of 2

Thread: QGraphicsView issues with panning and scaling

  1. #1
    Join Date
    May 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsView issues with panning and scaling

    Hi!

    I'm coming from experience with Cocoa and Winforms, and I'm just starting with Qt. I have set up a subclass of QGraphicsView. I'll show the relevant functions below. The issue I'm having is twofold:

    If I click item 1 and drag, item 2 moves. If I click item 2 and drag, item 1 moves.

    If I use the mousewheel to zoom, the mouseMoveEvent starts being called whenever the mouse moves even if a button is not held.

    I've been staring at this for over an hour, having tried different things, like forcing mouseTracking to false at the end of the wheelevent, which didn't change anything.

    Qt Code:
    1. EditorView::EditorView(QWidget* parent) :
    2. {
    3. setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    4.  
    5. QGraphicsScene* scene = new QGraphicsScene(this);
    6. setScene(scene);
    7. setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
    8.  
    9. item1->setRect(250.0, 250.0, 50.0, 50.0);
    10. item1->setFlag(QGraphicsItem::ItemIsMovable);
    11.  
    12. item2->setRect(150.0, 150.0, 50.0, 50.0);
    13. item2->setFlag(QGraphicsItem::ItemIsMovable);
    14.  
    15. setCursor(Qt::OpenHandCursor);
    16. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    17. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    18. }
    19.  
    20. void EditorView::mousePressEvent(QMouseEvent* event)
    21. {
    22. QGraphicsView::mousePressEvent(event);
    23.  
    24. m_lastDragPos = event->pos();
    25. }
    26.  
    27. void EditorView::mouseMoveEvent(QMouseEvent* event)
    28. {
    29. QPointF delta = mapToScene(event->pos()) - mapToScene(m_lastDragPos);
    30. PanView(delta);
    31. m_lastDragPos = event->pos();
    32. }
    33.  
    34. void EditorView::wheelEvent(QWheelEvent* event)
    35. {
    36. int delta = event->delta() / 120;
    37. ScaleView(delta);
    38. }
    39.  
    40. void EditorView::PanView(QPointF delta)
    41. {
    42. QPoint viewCenter(viewport()->width() / 2 - delta.x(), viewport()->height() / 2 - delta.y());
    43. QPointF newCenter = mapToScene(viewCenter);
    44. centerOn(newCenter);
    45. }
    46.  
    47. void EditorView::ScaleView(int delta)
    48. {
    49. bool zoomIn = (delta > 0);
    50. delta = abs(delta);
    51. ViewportAnchor oldAnchor = transformationAnchor();
    52. setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    53. for(int i = 0; i < delta; ++i)
    54. {
    55. if(zoomIn)
    56. {
    57. scale(1.10, 1.10);
    58. }
    59. else
    60. {
    61. scale(0.90, 0.90);
    62. }
    63. }
    64. setTransformationAnchor(oldAnchor);
    65. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView issues with panning and scaling

    Quite a few people looked in here, thanks for that. So I'll post what was going on now that I've figured it out.

    It turns out in mousePressEvent I was calling the superclass version of that, and it was triggering the default panning. So clicking item1 and dragging moved item1, but panned at the same time. So I would see item2 move, and item1 stay still. I figured this out by adding item3, and I'd see both items 2 and 3 moving when click-dragging item1.

    About the attaching to the cursor, it seems to have been related to the use of the AnchorUnderMouse thing. Setting it off that at the bottom of the function does not appear to have worked properly. I've changed how the zoom-while-keeping-the-point-under-the-mouse-still thing works, and that part is fixed as well.

    Now to get all this working AND get the default dragging behavior from the items, which I have now lost.

Similar Threads

  1. Replies: 7
    Last Post: 23rd November 2011, 20:13
  2. Replies: 1
    Last Post: 20th July 2010, 12:16
  3. QGraphicsView and panning performance
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2009, 19:40
  4. Scaling of pen width in QGraphicsView
    By spuds in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2008, 01:47
  5. widget qgraphicsview scaling
    By mistertoony in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2007, 22:46

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.