Results 1 to 2 of 2

Thread: QGrahicsItem: problem with ItemIsMovable (dragging rectangle bigger than item itself)

  1. #1
    Join Date
    Mar 2011
    Qt products

    Default QGrahicsItem: problem with ItemIsMovable (dragging rectangle bigger than item itself)


    I have a problem with a QGraphicsItem in a QGraphicsScene. What I would like is to be able to move the item with mouse. But the problem is that in my case, the dragging rectangle is bigger than the item itself.

    Here is the code that I use:
    Qt Code:
    1. class Test: public QGraphicsView
    2. {
    3. public:
    4. Test();
    6. private:
    7. virtual void resizeEvent (QResizeEvent * event);
    8. QGraphicsScene* m_pScene;
    9. };
    11. Test::Test()
    12. {
    13. m_pScene = new QGraphicsScene();
    14. setScene(m_pScene);
    16. m_pScene->setSceneRect(0, 0, 100, 100);
    18. for (int i = 0 ; i < 10 ; i++)
    19. {
    20. pItem->setFlag(QGraphicsItem::ItemIsMovable);
    21. pItem->setBrush(QBrush(QColor(190, 100, 100)));
    22. pItem->setRect(QRectF(10*i, 10, 5, 80.f));
    23. pItem->setCursor(Qt::SizeAllCursor);
    24. m_pScene->addItem(pItem);
    25. }
    28. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    29. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    31. resize(600, 200);
    32. fitInView(scene()->sceneRect());
    33. show();
    34. }
    36. void Test::resizeEvent(QResizeEvent * event)
    37. {
    38. QGraphicsView::resizeEvent(event);
    39. fitInView(scene()->sceneRect());
    40. }
    To copy to clipboard, switch view to plain text mode 

    So when I run my program I have this window, and I can drag items. All seems ok.

    But if I look closer the dragging zone is bigger than the item itself. (see the blue rectangle on following screenshot) The blue rectangle means that If I move the mouse in this rectangle, the cursor changes, and I can drag the item.

    I have the feeling that the problem is related to the "fitInView(scene()->sceneRect());" line. If I remove the fitInView but adds a scale(5,1) for example, there is the same problem.

    Do you have an idea of what the problem could be? Am I doing something wring with coordinates?

  2. #2
    Join Date
    Jan 2006
    Munich, Germany
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows

    Default Re: QGrahicsItem: problem with ItemIsMovable (dragging rectangle bigger than item its

    what is the bounding rect that the item returns?
    Is it the same as when you set it?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Dragging a rectangle across a QGraphicsView
    By blooglet in forum Qt Programming
    Replies: 2
    Last Post: 16th November 2010, 10:19
  2. Tree View Item selected rectangle
    By LynneV in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2010, 16:17
  3. How change selected item rectangle on QGraphicsScene
    By xray2000 in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2010, 16:43
  4. Replies: 11
    Last Post: 6th March 2009, 22:15
  5. Dragging an item from QTableView to a QPushButton
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 23rd May 2007, 11:57

Tags for this Thread


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.