Results 1 to 4 of 4

Thread: vertical keypressevent not working on graphicsitem

  1. #1
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default vertical keypressevent not working on graphicsitem

    I'm currently working on a program for making graphs(figure 1), but I'm running into some problems when using the itemChange and KeyPressEvent from the QGraphicsItem class.

    I'm trying to implement a "snap to grid" feature in the map. It works when I move the nodes with the mouse, but not when using the keyboard arrows. Specifically moving up and down with the keyboard doesn't work. There is no problem in moving the node left and right with the keyboard. Moving a node up and down only works when I remove the "return newPos" line. I don't understand why.

    The code I'm using can be found a little further down.

    figure 1: Map Editor program


    Qt Code:
    1. QVariant Node::itemChange(GraphicsItemChange change, const QVariant& value)
    2. {
    3. if(change == ItemPositionChange && scene())
    4. {
    5. //value is the new position.
    6. QPointF newPos = value.toPointF();
    7. if(static_cast<int>(x() - newPos.x()) % GridScene::GridSize == 0)
    8. {
    9. newPos.setX(newPos.rx());
    10. newPos.setY(y());
    11. }
    12. else if(static_cast<int>(y() - newPos.y()) % GridScene::GridSize == 0)
    13. {
    14. newPos.setX(x());
    15. newPos.setY(newPos.ry());
    16. }
    17. else
    18. {
    19. newPos.setX(x());
    20. newPos.setY(y());
    21. }
    22.  
    23. updateToolTip();
    24.  
    25. return newPos;
    26.  
    27. }
    28.  
    29. return QGraphicsItem::itemChange(change, value);
    30. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Node::keyPressEvent(QKeyEvent* event)
    2. {
    3. switch(event->key())
    4. {
    5. case Qt::Key_Left:
    6. moveBy(-GridScene::GridSize, 0);
    7. break;
    8.  
    9. case Qt::Key_Right:
    10. moveBy(GridScene::GridSize, 0);
    11. break;
    12.  
    13. case Qt::Key_Up:
    14. moveBy(0, -GridScene::GridSize);
    15. break;
    16.  
    17. case Qt::Key_Down:
    18. moveBy(0, GridScene::GridSize);
    19. break;
    20.  
    21. default:
    22. QGraphicsItem::keyPressEvent(event);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vertical keypressevent not working on graphicsitem

    Why not simplify itemChange to:
    Qt Code:
    1. QVariant Node::itemChange(GraphicsItemChange change, const QVariant& value)
    2. {
    3. if(change == ItemPositionChange && scene())
    4. {
    5. //value is the new position.
    6. QPoint newPos = value.toPointF(); // note - no "F"
    7. QPoint offset(newPos.x() % GridScene::GridSize, newPos.y() % GridScene::GridSize);
    8. // if you want real positions, substitute mod with a different operation to calculate offset
    9. newPos -= offset;
    10. QPoint corr;
    11. if(offset.x()>0.5*GridScene::GridSize) corr.setX(GridScene::GridSize);
    12. if(offset.y()>0.5*GridScene::GridSize) corr.setY(GridScene::GridSize);
    13. newPos += corr;
    14. return newPos;
    15. }
    16. return QGraphicsItem::itemChange(change, value);
    17. }
    To copy to clipboard, switch view to plain text mode 
    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.


  3. The following user says thank you to wysota for this useful post:

    creatio.x (24th December 2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vertical keypressevent not working on graphicsitem

    wysota thank you. It seems to be working

  5. #4
    Join Date
    Mar 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vertical keypressevent not working on graphicsitem

    hmm, now I have another problem. The edge(line) in the scene doesn't snap to the grid. It moves with the size of GridScene::Gridsize, but not on the lines. See figure 2

    figure 2

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.