Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Populating Menu through Component

    QML and Qt Quick
    2
    3
    380
    Loading More Posts
    • 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.
    • L
      levolex last edited by levolex

      Hello,
      I've got problems trying to dynamically add menu items through Compopent element. I've tried several different ways (using Component element, creating Component dynamically...), but the only it works was population of Menu using Instantiator, in all other cases new items show memory addresses as a title and onTriggered() slot does nothing.

      I can modify my code to use model and Instatiator, but I still wonder why it doesn't work through Component or what I'm doing wrong.

      Here is my code:

      import QtQuick 2.7
      import QtQuick.Controls 1.4
      import QtQuick.Layouts 1.3
      
      ApplicationWindow {
        id: mainWindow
        visible: true
      
        menuBar: MenuBar {
          Menu {
              id: reportsMenu
              title: "Reports"
          }
        }
      
        Component {
            id: menuItemComp
            MenuItem {
            }
        }
        Component.onCompleted: {
            reportsMenu.addItem(menuItemComp.createObject(reportsMenu, { text: "testing" }));
        }
      }
      

      Here is result:
      0_1525328754700_dynamic_menu.png

      1 Reply Last reply Reply Quote 0
      • GrecKo
        GrecKo Qt Champions 2018 last edited by

        addItem takes a string parameter and return a newly created MenuItem.

        1 Reply Last reply Reply Quote 2
        • L
          levolex last edited by

          @GrecKo said in Populating Menu through Component:

          t

          You are so right. It was not correct functioning of QtDesigner context help and my inattention. Context help showed me always the article of component from Qt.labs.platform module. Thank you so much.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post