Results 1 to 14 of 14

Thread: Problem with qgraphicsitem_Cast

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with qgraphicsitem_Cast

    Quote Originally Posted by JohannesMunk View Post
    The problem is that you somehow need to save the relative position of the mousecursor and the item when you start moving. If you just set the position of the item to the mouseCursors position you will move the items origin (0,0) to that position and not the center or wherever you might have clicked.

    There are several ways to do that:

    1) only use deltas:
    item->setPos(item->pos() + event->scenePos() - event->lastScenePos() );

    2) store everything:
    Wherever you start your move action, store in member variables 1) the item-scenePos (startItemPos) and 2) the event-scenePos (startMousePos). If you want to support moving multiple items, store a list of their startPositions. In mouseMove set the item-position to: item->setPos( startItemPos + event->scenePos() - startMousePos);

    HIH

    Joh
    I am not using the method drug 'n drop but the point 'n click (i select the "piece" and then i click there which i wanted to be moved....) I tried the 1) but the piece doesn't do nothing...because it is moving in the same position....

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qgraphicsitem_Cast

    I understand your point'n'click well.. that just changes where you start stuff.

    The piece does nothing? What do you mean same position?

  3. #3
    Join Date
    Aug 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with qgraphicsitem_Cast

    Quote Originally Posted by JohannesMunk View Post
    I understand your point'n'click well.. that just changes where you start stuff.

    The piece does nothing? What do you mean same position?
    i use the 1) method....I mean that it moves but it goes to the same place....(It is something wrong with the "item->pos() + event->scenePos() - event->lastScenePos() ") because the piece is moving into the current position....E.X. if it is into the (210,150) before the click, after it will be into the (210,150)....So it moves but it goes-reach into the same place....

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with qgraphicsitem_Cast

    The code is fine, but you need to enable Mousetracking and hoverEvents:

    Qt Code:
    1. class Scene : public QGraphicsScene
    2. {
    3. public:
    4. Scene() {underMouse = 0;}
    5. protected:
    6. void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent )
    7. {
    8. if (underMouse != 0)
    9. {
    10. underMouse->setPos(underMouse->pos() + mouseEvent->scenePos() - mouseEvent->lastScenePos());
    11. qDebug() << mouseEvent->scenePos() - mouseEvent->lastScenePos();
    12. }
    13. QGraphicsScene::mouseMoveEvent(mouseEvent);
    14. }
    15.  
    16. void mouseReleaseEvent ( QGraphicsSceneMouseEvent * mouseEvent )
    17. {
    18. if (underMouse == 0)
    19. underMouse = itemAt(mouseEvent->scenePos());
    20. else
    21. underMouse = 0;
    22. QGraphicsScene::mouseReleaseEvent(mouseEvent);
    23. }
    24.  
    25. private:
    26. QGraphicsItem* underMouse;
    27. };
    28.  
    29. class Piece : public QGraphicsItem
    30. {
    31. public:
    32. Piece() {
    33. setAcceptHoverEvents(true);
    34. }
    35. QRectF boundingRect() const{ return QRectF(0, 0, 50, 50); }
    36.  
    37. void paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
    38. {
    39. painter->drawRect(boundingRect());
    40. }
    41. };
    42.  
    43.  
    44. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    45. {
    46. scene = new Scene();
    47.  
    48. Piece* piece = new Piece();
    49. scene->addItem(piece);
    50.  
    51. graphicsView = new QGraphicsView(this);
    52. graphicsView->setMouseTracking(true);
    53.  
    54. graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    55. graphicsView->setScene(scene);
    56. graphicsView->show();
    57.  
    58. graphicsView->setGeometry(QRect(50, 50, 500, 300));
    59.  
    60. this->setCentralWidget(graphicsView);
    61.  
    62. }
    To copy to clipboard, switch view to plain text mode 

    Joh

Similar Threads

  1. QGraphicsItemPrivate and qgraphicsitem_cast
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2009, 08:13
  2. qgraphicsitem_cast problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2008, 15:22
  3. Improving qgraphicsitem_cast
    By Gopala Krishna in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2007, 17:07
  4. regarding qgraphicsitem_cast
    By Gopala Krishna in forum Qt Programming
    Replies: 6
    Last Post: 23rd December 2006, 15:09

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
  •  
Qt is a trademark of The Qt Company.