Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Move rectangle with arrow keys
Forum Updated to NodeBB v4.3 + New Features

Move rectangle with arrow keys

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neda
    wrote on last edited by neda
    #1

    How would I use the arrow keys to move rectangle?

    Rectangle {
            id:point
            anchors.centerIn: parent
            width: 20
            height: 20
    
            opacity: 0.2
    
            MouseArea {
                anchors.fill: parent
                drag.target: root
                onPositionChanged: {
                    if(drag.active) {
                        dragged()
                    }
                }
    
                
            }
    Keys.onPressed: {
                   if (event.key == Qt.Key_Left) {
                        console.log("move left");
                        event.accepted = true;
                        point.x-=1;
                    }
                }
        }
    
    
    p3c0P 1 Reply Last reply
    0
    • N neda

      How would I use the arrow keys to move rectangle?

      Rectangle {
              id:point
              anchors.centerIn: parent
              width: 20
              height: 20
      
              opacity: 0.2
      
              MouseArea {
                  anchors.fill: parent
                  drag.target: root
                  onPositionChanged: {
                      if(drag.active) {
                          dragged()
                      }
                  }
      
                  
              }
      Keys.onPressed: {
                     if (event.key == Qt.Key_Left) {
                          console.log("move left");
                          event.accepted = true;
                          point.x-=1;
                      }
                  }
          }
      
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @neda Try without anchoring.

      157

      N 1 Reply Last reply
      0
      • p3c0P p3c0

        @neda Try without anchoring.

        N Offline
        N Offline
        neda
        wrote on last edited by
        #3

        @p3c0

        Thank you. but it does not work.
        Please see this link.

        p3c0P 1 Reply Last reply
        0
        • N neda

          @p3c0

          Thank you. but it does not work.
          Please see this link.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @neda AFAIK an anchored Item will not move unless you free it.

          Rectangle {
                  id:point
                  anchors.centerIn: parent // <- remove this
                  width: 20
                  ...
          

          I don't understand what you are trying to do inside canvas. But for a test I tried the rest of the code. It works by applying the above said changes. It will move the first Item.

          157

          N 1 Reply Last reply
          0
          • p3c0P p3c0

            @neda AFAIK an anchored Item will not move unless you free it.

            Rectangle {
                    id:point
                    anchors.centerIn: parent // <- remove this
                    width: 20
                    ...
            

            I don't understand what you are trying to do inside canvas. But for a test I tried the rest of the code. It works by applying the above said changes. It will move the first Item.

            N Offline
            N Offline
            neda
            wrote on last edited by
            #5

            @p3c0
            Thank you, but it does not work. I put my project in this link

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved