The Shortcut in a PySide2 MenuItem prevents window from showing












0















I'm trying to create a very simple ApplicationWindow using PySide2 (Qt for Windows) and QML.



main.py



import sys
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QUrl
from PySide2.QtQml import QQmlApplicationEngine

if __name__ == "__main__":
app = QApplication(sys.argv)
url = QUrl("mainWindow.qml")
engine = QQmlApplicationEngine()
engine.load(url)
sys.exit(app.exec_())


qml file



import QtQuick.Controls 2.4

ApplicationWindow {
id: mainWindow
visible: true
title: "MainWindow"
width: 640
height: 480

menuBar: MenuBar {
id: menuBar

Menu {
id: editMenu
title: "&Edit"

MenuItem {
id: copyItem
text: "Copy"
// This doesn't work:
// shortcut: "Ctrl+C"
// This doesn't work either:
// shortcut: StandardKey.Copy
}
}
}
}


As shown, the code runs and displays an ApplicationWindow with a MenuBar and the Menu. But if I outcomment either of the two shortcut variants the window isn't shown at all. I don't understand, why. My example follows the Qt documentation on MenuItems.










share|improve this question





























    0















    I'm trying to create a very simple ApplicationWindow using PySide2 (Qt for Windows) and QML.



    main.py



    import sys
    from PySide2.QtWidgets import QApplication
    from PySide2.QtCore import QUrl
    from PySide2.QtQml import QQmlApplicationEngine

    if __name__ == "__main__":
    app = QApplication(sys.argv)
    url = QUrl("mainWindow.qml")
    engine = QQmlApplicationEngine()
    engine.load(url)
    sys.exit(app.exec_())


    qml file



    import QtQuick.Controls 2.4

    ApplicationWindow {
    id: mainWindow
    visible: true
    title: "MainWindow"
    width: 640
    height: 480

    menuBar: MenuBar {
    id: menuBar

    Menu {
    id: editMenu
    title: "&Edit"

    MenuItem {
    id: copyItem
    text: "Copy"
    // This doesn't work:
    // shortcut: "Ctrl+C"
    // This doesn't work either:
    // shortcut: StandardKey.Copy
    }
    }
    }
    }


    As shown, the code runs and displays an ApplicationWindow with a MenuBar and the Menu. But if I outcomment either of the two shortcut variants the window isn't shown at all. I don't understand, why. My example follows the Qt documentation on MenuItems.










    share|improve this question



























      0












      0








      0








      I'm trying to create a very simple ApplicationWindow using PySide2 (Qt for Windows) and QML.



      main.py



      import sys
      from PySide2.QtWidgets import QApplication
      from PySide2.QtCore import QUrl
      from PySide2.QtQml import QQmlApplicationEngine

      if __name__ == "__main__":
      app = QApplication(sys.argv)
      url = QUrl("mainWindow.qml")
      engine = QQmlApplicationEngine()
      engine.load(url)
      sys.exit(app.exec_())


      qml file



      import QtQuick.Controls 2.4

      ApplicationWindow {
      id: mainWindow
      visible: true
      title: "MainWindow"
      width: 640
      height: 480

      menuBar: MenuBar {
      id: menuBar

      Menu {
      id: editMenu
      title: "&Edit"

      MenuItem {
      id: copyItem
      text: "Copy"
      // This doesn't work:
      // shortcut: "Ctrl+C"
      // This doesn't work either:
      // shortcut: StandardKey.Copy
      }
      }
      }
      }


      As shown, the code runs and displays an ApplicationWindow with a MenuBar and the Menu. But if I outcomment either of the two shortcut variants the window isn't shown at all. I don't understand, why. My example follows the Qt documentation on MenuItems.










      share|improve this question
















      I'm trying to create a very simple ApplicationWindow using PySide2 (Qt for Windows) and QML.



      main.py



      import sys
      from PySide2.QtWidgets import QApplication
      from PySide2.QtCore import QUrl
      from PySide2.QtQml import QQmlApplicationEngine

      if __name__ == "__main__":
      app = QApplication(sys.argv)
      url = QUrl("mainWindow.qml")
      engine = QQmlApplicationEngine()
      engine.load(url)
      sys.exit(app.exec_())


      qml file



      import QtQuick.Controls 2.4

      ApplicationWindow {
      id: mainWindow
      visible: true
      title: "MainWindow"
      width: 640
      height: 480

      menuBar: MenuBar {
      id: menuBar

      Menu {
      id: editMenu
      title: "&Edit"

      MenuItem {
      id: copyItem
      text: "Copy"
      // This doesn't work:
      // shortcut: "Ctrl+C"
      // This doesn't work either:
      // shortcut: StandardKey.Copy
      }
      }
      }
      }


      As shown, the code runs and displays an ApplicationWindow with a MenuBar and the Menu. But if I outcomment either of the two shortcut variants the window isn't shown at all. I don't understand, why. My example follows the Qt documentation on MenuItems.







      python qml qtquick2 shortcut pyside2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 15:51









      eyllanesc

      75.6k103156




      75.6k103156










      asked Nov 13 '18 at 15:22









      rengelrengel

      1931210




      1931210
























          1 Answer
          1






          active

          oldest

          votes


















          1














          In QML there are 2 types of items: Qt Quick Controls 1 and Qt Quick Controls 2. Both groups have items with the same name but they differ in their properties, in your case MenuItem of Qt Quick Controls 2 does not have a shortcut property but instead Qt Quick Controls 1 if it has it so the solution is to change the import:



          import QtQuick 2.11         // <---
          import QtQuick.Controls 1.4 // <---

          ApplicationWindow {
          id: mainWindow
          visible: true
          title: "MainWindow"
          width: 640
          height: 480

          menuBar: MenuBar {
          id: menuBar
          Menu {
          id: editMenu
          title: "&Edit"

          MenuItem {
          id: copyItem
          text: "Copy"
          shortcut: StandardKey.Copy
          onTriggered: console.log("copy")
          }
          }
          }
          }





          share|improve this answer
























          • Thanks! Works perfectly now.

            – rengel
            Nov 13 '18 at 16:22











          • @rengel If my answer helps you do not forget to mark it as correct, that's the best way to thank, if you do not know how to do it please check the tour

            – eyllanesc
            Nov 13 '18 at 16:24











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284190%2fthe-shortcut-in-a-pyside2-menuitem-prevents-window-from-showing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In QML there are 2 types of items: Qt Quick Controls 1 and Qt Quick Controls 2. Both groups have items with the same name but they differ in their properties, in your case MenuItem of Qt Quick Controls 2 does not have a shortcut property but instead Qt Quick Controls 1 if it has it so the solution is to change the import:



          import QtQuick 2.11         // <---
          import QtQuick.Controls 1.4 // <---

          ApplicationWindow {
          id: mainWindow
          visible: true
          title: "MainWindow"
          width: 640
          height: 480

          menuBar: MenuBar {
          id: menuBar
          Menu {
          id: editMenu
          title: "&Edit"

          MenuItem {
          id: copyItem
          text: "Copy"
          shortcut: StandardKey.Copy
          onTriggered: console.log("copy")
          }
          }
          }
          }





          share|improve this answer
























          • Thanks! Works perfectly now.

            – rengel
            Nov 13 '18 at 16:22











          • @rengel If my answer helps you do not forget to mark it as correct, that's the best way to thank, if you do not know how to do it please check the tour

            – eyllanesc
            Nov 13 '18 at 16:24
















          1














          In QML there are 2 types of items: Qt Quick Controls 1 and Qt Quick Controls 2. Both groups have items with the same name but they differ in their properties, in your case MenuItem of Qt Quick Controls 2 does not have a shortcut property but instead Qt Quick Controls 1 if it has it so the solution is to change the import:



          import QtQuick 2.11         // <---
          import QtQuick.Controls 1.4 // <---

          ApplicationWindow {
          id: mainWindow
          visible: true
          title: "MainWindow"
          width: 640
          height: 480

          menuBar: MenuBar {
          id: menuBar
          Menu {
          id: editMenu
          title: "&Edit"

          MenuItem {
          id: copyItem
          text: "Copy"
          shortcut: StandardKey.Copy
          onTriggered: console.log("copy")
          }
          }
          }
          }





          share|improve this answer
























          • Thanks! Works perfectly now.

            – rengel
            Nov 13 '18 at 16:22











          • @rengel If my answer helps you do not forget to mark it as correct, that's the best way to thank, if you do not know how to do it please check the tour

            – eyllanesc
            Nov 13 '18 at 16:24














          1












          1








          1







          In QML there are 2 types of items: Qt Quick Controls 1 and Qt Quick Controls 2. Both groups have items with the same name but they differ in their properties, in your case MenuItem of Qt Quick Controls 2 does not have a shortcut property but instead Qt Quick Controls 1 if it has it so the solution is to change the import:



          import QtQuick 2.11         // <---
          import QtQuick.Controls 1.4 // <---

          ApplicationWindow {
          id: mainWindow
          visible: true
          title: "MainWindow"
          width: 640
          height: 480

          menuBar: MenuBar {
          id: menuBar
          Menu {
          id: editMenu
          title: "&Edit"

          MenuItem {
          id: copyItem
          text: "Copy"
          shortcut: StandardKey.Copy
          onTriggered: console.log("copy")
          }
          }
          }
          }





          share|improve this answer













          In QML there are 2 types of items: Qt Quick Controls 1 and Qt Quick Controls 2. Both groups have items with the same name but they differ in their properties, in your case MenuItem of Qt Quick Controls 2 does not have a shortcut property but instead Qt Quick Controls 1 if it has it so the solution is to change the import:



          import QtQuick 2.11         // <---
          import QtQuick.Controls 1.4 // <---

          ApplicationWindow {
          id: mainWindow
          visible: true
          title: "MainWindow"
          width: 640
          height: 480

          menuBar: MenuBar {
          id: menuBar
          Menu {
          id: editMenu
          title: "&Edit"

          MenuItem {
          id: copyItem
          text: "Copy"
          shortcut: StandardKey.Copy
          onTriggered: console.log("copy")
          }
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 16:02









          eyllanesceyllanesc

          75.6k103156




          75.6k103156













          • Thanks! Works perfectly now.

            – rengel
            Nov 13 '18 at 16:22











          • @rengel If my answer helps you do not forget to mark it as correct, that's the best way to thank, if you do not know how to do it please check the tour

            – eyllanesc
            Nov 13 '18 at 16:24



















          • Thanks! Works perfectly now.

            – rengel
            Nov 13 '18 at 16:22











          • @rengel If my answer helps you do not forget to mark it as correct, that's the best way to thank, if you do not know how to do it please check the tour

            – eyllanesc
            Nov 13 '18 at 16:24

















          Thanks! Works perfectly now.

          – rengel
          Nov 13 '18 at 16:22





          Thanks! Works perfectly now.

          – rengel
          Nov 13 '18 at 16:22













          @rengel If my answer helps you do not forget to mark it as correct, that's the best way to thank, if you do not know how to do it please check the tour

          – eyllanesc
          Nov 13 '18 at 16:24





          @rengel If my answer helps you do not forget to mark it as correct, that's the best way to thank, if you do not know how to do it please check the tour

          – eyllanesc
          Nov 13 '18 at 16:24


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284190%2fthe-shortcut-in-a-pyside2-menuitem-prevents-window-from-showing%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python