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. How to use key sequences in qml ?

How to use key sequences in qml ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 3.2k Views 3 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.
  • small_birdS Offline
    small_birdS Offline
    small_bird
    wrote on last edited by
    #1

    Hello, everyone! Now, I'd like to use Ctrl+Z to do the undo practice. However, how to do it in qml ? Does anyone know the answer ? Thanks in advance.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by sierdzio
      #2

      There is Qt::Key_Undo available in KeyEvent.

      Some more example uses are listed in Keys component docs - link.

      (Z(:^

      small_birdS 1 Reply Last reply
      2
      • sierdzioS sierdzio

        There is Qt::Key_Undo available in KeyEvent.

        Some more example uses are listed in Keys component docs - link.

        small_birdS Offline
        small_birdS Offline
        small_bird
        wrote on last edited by small_bird
        #3

        @sierdzio Thanks a lot! However, I have not found Qt.Undo or something else. How could I use it in qml ?

        1 Reply Last reply
        1
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4
          Rectangle { // or anything else
            Keys.onPressed: {
              if (event.key == Qt.Key_Undo) {
                // do your undo handling here
                event.accepted = true;
             }
            }
          }
          

          If you ask about undo/redo handling in text edit fields - it is handled automatically. If you need some more advanced functionality, use the C++ part for that: QUndoCOmmand and friends. http://doc.qt.io/qt-5/qundo.html

          (Z(:^

          small_birdS 1 Reply Last reply
          1
          • sierdzioS sierdzio
            Rectangle { // or anything else
              Keys.onPressed: {
                if (event.key == Qt.Key_Undo) {
                  // do your undo handling here
                  event.accepted = true;
               }
              }
            }
            

            If you ask about undo/redo handling in text edit fields - it is handled automatically. If you need some more advanced functionality, use the C++ part for that: QUndoCOmmand and friends. http://doc.qt.io/qt-5/qundo.html

            small_birdS Offline
            small_birdS Offline
            small_bird
            wrote on last edited by
            #5

            @sierdzio There is not a Qt.Key_Undo value. How could you get that?

            1 Reply Last reply
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              As said on chat, if Key_Undo is not exported, you can either handle it in c++ or react to Key_Z and check if modifier is ctrl.

              (Z(:^

              small_birdS 1 Reply Last reply
              0
              • jpnurmiJ Offline
                jpnurmiJ Offline
                jpnurmi
                wrote on last edited by jpnurmi
                #7

                For a focused item, you can use KeyEvent::matches():

                Item {
                    focus: true
                    Keys.onPressed: {
                        if (event.matches(StandardKey.Undo))
                            myModel.undo()
                    }
                }
                

                If you want to handle it as a shortcut, regardless of which item has focus, use QML Shortcut:

                Shortcut {
                    sequence: StandardKey.Undo
                    onActivated: myModel.undo()
                }
                
                sierdzioS 1 Reply Last reply
                3
                • sierdzioS sierdzio

                  As said on chat, if Key_Undo is not exported, you can either handle it in c++ or react to Key_Z and check if modifier is ctrl.

                  small_birdS Offline
                  small_birdS Offline
                  small_bird
                  wrote on last edited by
                  #8

                  @sierdzio Thanks a lot, it works.

                  1 Reply Last reply
                  1
                  • jpnurmiJ jpnurmi

                    For a focused item, you can use KeyEvent::matches():

                    Item {
                        focus: true
                        Keys.onPressed: {
                            if (event.matches(StandardKey.Undo))
                                myModel.undo()
                        }
                    }
                    

                    If you want to handle it as a shortcut, regardless of which item has focus, use QML Shortcut:

                    Shortcut {
                        sequence: StandardKey.Undo
                        onActivated: myModel.undo()
                    }
                    
                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #9

                    @jpnurmi said in How to use key sequences in qml ?:

                    For a focused item, you can use KeyEvent::matches()
                    [...]
                    If you want to handle it as a shortcut, regardless of which item has focus, use QML Shortcut [...]

                    This is pure gold! Thanks!

                    (Z(:^

                    1 Reply Last reply
                    2

                    • Login

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