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
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: Problem with qgraphicsitem_Cast

    How about subclassing QGraphicsScene.

    Reimplement mousePressEvent, mouseMoveEvent, mouseReleaseEvent accordingly.

    use scene.itemAt( .. ) with the mouseEvent position.

    if you want you can either use the scenes.selectedItems or manage the selection yourself.

    HIH

    Johannes

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

    Default Re: Problem with qgraphicsitem_Cast

    I need the qgraphicsitem_cast because i am using the scene.items() with the Q_FOREACH to create a loop with all the items of the scene....And the qgraphicsitem_cast because i want to use on them some custom functions that i have created....

    Quote Originally Posted by JohannesMunk View Post
    How about subclassing QGraphicsScene.

    Reimplement mousePressEvent, mouseMoveEvent, mouseReleaseEvent accordingly.

    use scene.itemAt( .. ) with the mouseEvent position.

    if you want you can either use the scenes.selectedItems or manage the selection yourself.

    HIH

    Johannes
    I did something like that but i have a small problem.... As regards my code everything works fine but when i am choosing a "piece" and i am clicking somewhere in the scene, then it moves but not in the place in which i have click but near to it....I have tried to move it with the following ways....But none of them doesn't work right....

    Qt Code:
    1. setPos(event->screenPos());
    2. setPos(event->pos());
    3. setPos(event->scenePos());
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Problem with qgraphicsitem_Cast

    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

  4. #4
    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....

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

    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?

  6. #6
    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....

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

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Problem with qgraphicsitem_Cast

    Quote Originally Posted by kokeroulis View Post
    I need the qgraphicsitem_cast because i am using the scene.items() with the Q_FOREACH to create a loop with all the items of the scene....And the qgraphicsitem_cast because i want to use on them some custom functions that i have created....
    You don't need this cast for that. Have all the items inherit a common (custom) subclass of QGraphicsItem and place all the methods in the common subclass as virtual methods and them reimplement them in the final classes. Then you'll be able to call them directly. Alternatively implement the methods in your scene and make overloads for them for each of your item classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Problem with qgraphicsitem_Cast

    Hello

    i have manage to do it...Thank you for your help....All of you...

Similar Threads

  1. QGraphicsItemPrivate and qgraphicsitem_cast
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2009, 09:13
  2. qgraphicsitem_cast problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2008, 16:22
  3. Improving qgraphicsitem_cast
    By Gopala Krishna in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2007, 18:07
  4. regarding qgraphicsitem_cast
    By Gopala Krishna in forum Qt Programming
    Replies: 6
    Last Post: 23rd December 2006, 16: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.