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. MenuBar: display shortcut in QuickControls2
Qt 6.11 is out! See what's new in the release blog

MenuBar: display shortcut in QuickControls2

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 4 Posters 3.1k 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by maxwell31
    #1

    Hi,

    I have a menu bar which looks like this:

    menuBar: MenuBar {
        id: mainmenu
        Menu {
            title: qsTr("&File")
            Action {
                text: qsTr("&Open...")
                shortcut: StandardKey.Open
                onTriggered: {
                    fileDialog.visible=true
                }
            }
         }
        Menu {
            title: qsTr("&View")
            Action {
                id: fullscreenAction
                checkable: true
                checked: false
                text: qsTr("&Fullscreen")
                shortcut: Shortcut {
                    sequences: [StandardKey.FullScreen,"F12"]
                    onActivated: {
                        fullscreenAction.toggle()
                    }
                }
    

    If I press Alt+F The File Menu expands. Is it possible to show the Keyboard shortcut next to the text (in the case of file open Ctrl+O)?

    1 Reply Last reply
    0
    • Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by Pradeep P N
      #2

      Hi @maxwell31

      • Please have a look at MenuItem which has a shortcut property.
      • You can provide action to the MenuItem

      Example:

      import QtQuick 2.9
      import QtQuick.Window 2.0
      import QtQuick.Controls 1.4
      
      ApplicationWindow  {
          visible: true
          width: 640
          height: 480
      
          menuBar: MenuBar {
              Menu {
                  title: "File"
      
                  MenuItem { text: "Open"; shortcut: "Ctrl+O"; }
      
                  MenuItem {
                      shortcut: "Ctrl+Q"
                      text: 'Close';
      
                      action: Action {
                          shortcut: StandardKey.Quit
      
                          onTriggered: {
                              Qt.quit();
                          }
                      }
                  }
              }
      
              Menu {
                  title: "Edit"
                  MenuItem { text: "Cut";  shortcut: "Ctrl+X"  }
                  MenuItem { text: "Copy"; shortcut: "Ctrl+C"  }
                  MenuItem { text: "Paste";  shortcut: "Ctrl+V"  }
              }
          }
      }
      
      

      0_1560835379934_Menu.gif

      All the best

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        maxwell31
        wrote on last edited by
        #3

        Ah, I see. Thank you. I thought about using The QuickControls2 menu, as QuickControls1 are already listed as deprecated. There the MenuItem does not have a shortcut property anymore. Is there a possibility with QuickControls2?

        J.HilkJ 1 Reply Last reply
        0
        • M maxwell31

          Ah, I see. Thank you. I thought about using The QuickControls2 menu, as QuickControls1 are already listed as deprecated. There the MenuItem does not have a shortcut property anymore. Is there a possibility with QuickControls2?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @maxwell31
          you can customize your MenuItem
          https://doc.qt.io/qt-5/qtquickcontrols2-customize.html

          Therefore, (untested)

          MenuItem {
              id:menuItem
          
             contentItem: Item {
           Text {
                      leftPadding: menuItem.indicator.width
                      rightPadding: menuItem.arrow.width
                      text: menuItem.text
                      font: menuItem.font
                      opacity: enabled ? 1.0 : 0.3
                      color: menuItem.highlighted ? "#ffffff" : "#21be2b"
                      horizontalAlignment: Text.AlignLeft
                      verticalAlignment: Text.AlignVCenter
                  }
          
              Text{
               //the shortcut
                      leftPadding: menuItem.indicator.width
                      rightPadding: menuItem.arrow.width
                      text: "STRG + Q"
                      font: menuItem.font
                      opacity: enabled ? 1.0 : 0.3
                      color: menuItem.highlighted ? "#ffffff" : "#21be2b"
                      horizontalAlignment: Text.AlignRight
                      verticalAlignment: Text.AlignVCenter
              }
             }
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • M Offline
            M Offline
            maxwell31
            wrote on last edited by maxwell31
            #5

            Thank you! Yes this will work. Do you know a way how to get the text corresponding to something like StandardKey.Fullscreen? It will be a cross platform application, therefore I don't want to hardcode the text.

            With e.g. StandardKey.Quit.toString() I can get the enum value, but how to get the string in qml?

            J.HilkJ 1 Reply Last reply
            0
            • M maxwell31

              Thank you! Yes this will work. Do you know a way how to get the text corresponding to something like StandardKey.Fullscreen? It will be a cross platform application, therefore I don't want to hardcode the text.

              With e.g. StandardKey.Quit.toString() I can get the enum value, but how to get the string in qml?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @maxwell31 I'm afraid not,

              you can decide the text on the OS, currently running.

              text: {
                   if(Qt.platform.os == "windows")
                      return "STRG + Q"
                   else if( Qt.platform.os == "osx")
                     return "⌘ + Q"
              }
              

              https://doc.qt.io/qt-5/qml-qtqml-qt.html#platform-prop


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • alex_spataruA Offline
                alex_spataruA Offline
                alex_spataru
                wrote on last edited by alex_spataru
                #7

                Elaborating on the answers provided so far, I came up with the following solution:

                Create a QML file named CustomMenuItem.qml with the following code:

                import QtQuick 2.12
                import QtQuick.Layouts 1.12
                import QtQuick.Controls 2.12
                
                MenuItem {
                    id: root
                
                    property alias sequence: _shortcut.sequence
                    property bool indicatorVisible: root.icon.source.length > 0 || root.checkable
                
                    Shortcut {
                        id: _shortcut
                        enabled: root.enabled
                        onActivated: root.triggered()
                    }
                
                    contentItem: RowLayout {
                        spacing: 0
                        width: root.width
                        opacity: root.enabled ? 1 : 0.5
                
                        Item {
                            width: root.indicatorVisible ? root.indicator.width + 4 : 0
                        }
                
                        Label {
                            text: root.text
                            Layout.fillWidth: true
                            elide: Label.ElideRight
                            verticalAlignment: Qt.AlignVCenter
                        }
                
                        Item {
                            Layout.fillWidth: true
                        }
                
                        Label {
                            text: _shortcut.nativeText
                            verticalAlignment: Qt.AlignVCenter
                        }
                    }
                }
                

                You can now use this item in your MenuBar as follows:

                MenuBar {
                    Menu {
                        title: qsTr("File")
                
                        CustomMenuItem {
                            sequence: StandardKey.Open
                            text: qsTr("Select JSON file") + "..."
                        }
                
                        // Rest of code... (which I still haven't done)
                    }
                }
                

                Screenshot:

                Screen Shot 2021-02-17 at 11.45.13.png

                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