Results 1 to 5 of 5

Thread: Move rectangle with arrow keys

  1. #1
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Move rectangle with arrow keys

    How would I use the arrow keys to move rectangle?

    Qt Code:
    1. Rectangle {
    2. id:point
    3. anchors.centerIn: parent
    4. width: 20
    5. height: 20
    6.  
    7. opacity: 0.2
    8.  
    9. MouseArea {
    10. anchors.fill: parent
    11. drag.target: root
    12. onPositionChanged: {
    13. if(drag.active) {
    14. dragged()
    15. }
    16. }
    17. }
    18. Keys.onPressed: {
    19. if (event.key == Qt.Key_Left) {
    20. console.log("move left");
    21. event.accepted = true;
    22. point.x-=1;
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Move rectangle with arrow keys

    Looks ok, what doesn't work?
    Is the onPressed() called but it doesn't move?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Move rectangle with arrow keys

    Quote Originally Posted by anda_skoa View Post
    Looks ok, what doesn't work?
    Is the onPressed() called but it doesn't move?

    _
    Yes, it does not work. I put my project in this link

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Move rectangle with arrow keys

    Quote Originally Posted by neda View Post
    Yes
    Actually no, Keys.onPressed() is never called. But fortunately you provided the code so I could see for myself.

    1) You need to activate the focus in MouseArea.onPressed
    2) You need to activate the focus by calling point.forceActiveFocus()

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    neda (8th May 2016)

  6. #5
    Join Date
    Jan 2016
    Posts
    81
    Thanks
    31
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Move rectangle with arrow keys

    Quote Originally Posted by anda_skoa View Post
    Actually no, Keys.onPressed() is never called. But fortunately you provided the code so I could see for myself.

    1) You need to activate the focus in MouseArea.onPressed
    2) You need to activate the focus by calling point.forceActiveFocus()

    _
    Thank you so much dear friend.

Similar Threads

  1. i need to use Alt+Ctrl+arrow keys combination
    By prasad.ece2 in forum Qt Programming
    Replies: 13
    Last Post: 27th September 2013, 07:34
  2. Disable QGraphicsView scrolling with arrow keys
    By sakya in forum Qt Programming
    Replies: 0
    Last Post: 9th September 2013, 11:45
  3. Detect Arrow Keys
    By Ebonair in forum Qt Programming
    Replies: 4
    Last Post: 1st October 2010, 03:55
  4. Replies: 2
    Last Post: 4th August 2010, 20:10
  5. How can I associate arrow keys?
    By Mariane in forum Newbie
    Replies: 2
    Last Post: 20th January 2006, 18:31

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.