Results 1 to 2 of 2

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

  1. #1
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

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

    Hello,

    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();
    5.  
    6. private:
    7. virtual void resizeEvent (QResizeEvent * event);
    8. QGraphicsScene* m_pScene;
    9. };
    10.  
    11. Test::Test()
    12. {
    13. m_pScene = new QGraphicsScene();
    14. setScene(m_pScene);
    15.  
    16. m_pScene->setSceneRect(0, 0, 100, 100);
    17.  
    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. }
    26.  
    27.  
    28. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    29. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    30.  
    31. resize(600, 200);
    32. fitInView(scene()->sceneRect());
    33. show();
    34. }
    35.  
    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.
    screen1.jpg

    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.
    screen2.jpg

    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
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    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!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    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

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.