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 do you call a function in a loader

How do you call a function in a loader

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

    I have a loader in my main.qml, and I have a ComboBox, I want to be able to call a function in ComboBox via a loader, but I cannot seem to find a way to do this, without a loader it works fine, it is the loader that I do not get, I use a menu to call the loader, I use the loader to change languages in my app, this I just a short example.

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.15
    
    ApplicationWindow
    {
        width: 640
        height: 480
        visible: true
        title: qsTr("Test Loader ComboBox")
        /************************************************
         * @brief MenuBar.
         * MenuBar
         ***********************************************/
        menuBar: MenuBar
        {
            id: myMenuBar
            /************************************************
             * @brief MenuBar File.
             * MenuBar
             ***********************************************/
            Menu
            {
                id: myMenuFile
                title: qsTr("&File")
                MenuItem
                {
                    text: qsTr("&First")
                    onTriggered:
                    {
                        theFindComboBox.setComboBox("First");
                        thisComboBoxLoader.setComboBox("First");
                    }
                }
                MenuItem
                {
                    text: qsTr("&Second")
                    onTriggered:
                    {
                        theFindComboBox.setComboBox("Second");
                        MyComboBox.setComboBox("Second");
                    }
                }
                MenuItem
                {
                    text: qsTr("&Third")
                    onTriggered:
                    {
                        theFindComboBox.setComboBox("Third");
                        loaderContainer.setComboBox("Third");
                    }
                }
                MenuItem
                {
                    text: qsTr("&reload")
                    onTriggered:
                    {
                        thisComboBoxLoader.reload();
                    }
                }
                MenuItem
                {
                    text: qsTr("&Quit")
                    onTriggered: Qt.quit()
                }
            } // end myMenuFile
        } // end MenuBar
        /************************************************
         * @brief MyComboBox theFindComboBox.
         * MyComboBox
         ***********************************************/
        MyComboBox
        {
            id: theFindComboBox
        }
        /************************************************
         * @brief Item for Loader.
         * Item
         ***********************************************/
        Item
        {
            id: loaderContainer
            y: 100
            Loader
            {
                id: thisComboBoxLoader
                /************************************************
                 * @brief thisComboBoxLoader.reload().
                 * reload
                 ***********************************************/
                function reload()
                {
                    source = "";
                    source = "MyComboBox.qml";
                    console.debug("Reloaded")
                }
                anchors.centerIn: parent
                source: "MyComboBox.qml"
            } // end Loader
        }
    } // end Item
    /******************************* End of File *********************************/
    
    

    And I have a ComboBox with a function to change the index

    import QtQuick 2.0
    import QtQuick.Controls 2.15
    import QtQml 2.15
    
    Item
    {
        id: theFindThisComboBox
        property alias thisComboBox: theComboBox
        ComboBox
        {
            id: theComboBox
            model: ListModel
            {
                id: theModel
                ListElement { text: "First" }
                ListElement { text: "Second" }
                ListElement { text: "Third" }
            }
        }
        function setComboBox(findThis)
        {
            thisComboBox.currentIndex = thisComboBox.find(findThis);
        }
        Component.onCompleted:
        {
            setComboBox("Second");
        }
    }
    /******************************* End of File *********************************/
    
    

    Thanks Flesh

    Jeffrey Scott Flesher PhD
    http//LightWizzard.com/

    jeremy_kJ 1 Reply Last reply
    0
    • F Flesh

      I have a loader in my main.qml, and I have a ComboBox, I want to be able to call a function in ComboBox via a loader, but I cannot seem to find a way to do this, without a loader it works fine, it is the loader that I do not get, I use a menu to call the loader, I use the loader to change languages in my app, this I just a short example.

      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.15
      
      ApplicationWindow
      {
          width: 640
          height: 480
          visible: true
          title: qsTr("Test Loader ComboBox")
          /************************************************
           * @brief MenuBar.
           * MenuBar
           ***********************************************/
          menuBar: MenuBar
          {
              id: myMenuBar
              /************************************************
               * @brief MenuBar File.
               * MenuBar
               ***********************************************/
              Menu
              {
                  id: myMenuFile
                  title: qsTr("&File")
                  MenuItem
                  {
                      text: qsTr("&First")
                      onTriggered:
                      {
                          theFindComboBox.setComboBox("First");
                          thisComboBoxLoader.setComboBox("First");
                      }
                  }
                  MenuItem
                  {
                      text: qsTr("&Second")
                      onTriggered:
                      {
                          theFindComboBox.setComboBox("Second");
                          MyComboBox.setComboBox("Second");
                      }
                  }
                  MenuItem
                  {
                      text: qsTr("&Third")
                      onTriggered:
                      {
                          theFindComboBox.setComboBox("Third");
                          loaderContainer.setComboBox("Third");
                      }
                  }
                  MenuItem
                  {
                      text: qsTr("&reload")
                      onTriggered:
                      {
                          thisComboBoxLoader.reload();
                      }
                  }
                  MenuItem
                  {
                      text: qsTr("&Quit")
                      onTriggered: Qt.quit()
                  }
              } // end myMenuFile
          } // end MenuBar
          /************************************************
           * @brief MyComboBox theFindComboBox.
           * MyComboBox
           ***********************************************/
          MyComboBox
          {
              id: theFindComboBox
          }
          /************************************************
           * @brief Item for Loader.
           * Item
           ***********************************************/
          Item
          {
              id: loaderContainer
              y: 100
              Loader
              {
                  id: thisComboBoxLoader
                  /************************************************
                   * @brief thisComboBoxLoader.reload().
                   * reload
                   ***********************************************/
                  function reload()
                  {
                      source = "";
                      source = "MyComboBox.qml";
                      console.debug("Reloaded")
                  }
                  anchors.centerIn: parent
                  source: "MyComboBox.qml"
              } // end Loader
          }
      } // end Item
      /******************************* End of File *********************************/
      
      

      And I have a ComboBox with a function to change the index

      import QtQuick 2.0
      import QtQuick.Controls 2.15
      import QtQml 2.15
      
      Item
      {
          id: theFindThisComboBox
          property alias thisComboBox: theComboBox
          ComboBox
          {
              id: theComboBox
              model: ListModel
              {
                  id: theModel
                  ListElement { text: "First" }
                  ListElement { text: "Second" }
                  ListElement { text: "Third" }
              }
          }
          function setComboBox(findThis)
          {
              thisComboBox.currentIndex = thisComboBox.find(findThis);
          }
          Component.onCompleted:
          {
              setComboBox("Second");
          }
      }
      /******************************* End of File *********************************/
      
      

      Thanks Flesh

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by jeremy_k
      #2

      @Flesh What have you tried, and what are the symptoms of it not working? That example is 131 lines of code, and leaves me wondering what the goal is. To pick a random portion:

      function reload()
                  {
                      source = "";
                      source = "MyComboBox.qml";
                      console.debug("Reloaded")
                  }
      

      How does setting Loader.source twice relate to the topic's question?

      Something more concise might help.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Flesh
        wrote on last edited by
        #3

        This the working parts of the problem, it works if it is not a loader, but fails to connect as a loader, there must be something I am missing about how the loader works as far as calling functions in it.

        Not sure what you mean about the source twice, but that part of the code works, you have to set it to undefined or null to cause a reload to take place, the problem is how do I call a function that is loaded from the source.

        Jeffrey Scott Flesher PhD
        http//LightWizzard.com/

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #4

          @Flesh said in How do you call a function in a loader:

          thisComboBoxLoader

          <id of loader>.item.<function name>()

          thisComboBoxLoader.item.reload()
          

          "item" is in the Loader docs btw.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          1
          • F Offline
            F Offline
            Flesh
            wrote on last edited by
            #5

            @Flesh said in How do you call a function in a loader:

                    MenuItem
                    {
                        text: qsTr("&First")
                        onTriggered:
                        {
                            theFindComboBox.setComboBox("First"); // **This Works**
                            thisComboBoxLoader.setComboBox("First"); // **I have tired many ways to call this but none work**
                        }
                    }
            

            Jeffrey Scott Flesher PhD
            http//LightWizzard.com/

            jeremy_kJ 1 Reply Last reply
            0
            • F Flesh

              @Flesh said in How do you call a function in a loader:

                      MenuItem
                      {
                          text: qsTr("&First")
                          onTriggered:
                          {
                              theFindComboBox.setComboBox("First"); // **This Works**
                              thisComboBoxLoader.setComboBox("First"); // **I have tired many ways to call this but none work**
                          }
                      }
              
              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by jeremy_k
              #6

              Note the item difference between the two lines:

              @Flesh said in How do you call a function in a loader:

              thisComboBoxLoader.setComboBox("First");

              @fcarney said in How do you call a function in a loader:

              thisComboBoxLoader.item.reload()

              thisComboBoxLoader is the Loader. thisComboBoxLoader.item is the object loaded by the Loader. Properties of the loaded object do not become properties of the Loader.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Flesh
                wrote on last edited by
                #7

                @jeremy_k said in How do you call a function in a loader:

                thisComboBoxLoader.item

                That works, thanks, I did not know about the item property, good to know.

                Jeffrey Scott Flesher PhD
                http//LightWizzard.com/

                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