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. Why my code is no longer working when moving it in another file?

Why my code is no longer working when moving it in another file?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 351 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.
  • jeanmilostJ Offline
    jeanmilostJ Offline
    jeanmilost
    wrote on last edited by
    #1

    I have the following code (I only posted the interesting part):

    TreeView.qml

    ...
    
    /**
    * Tree content model
    */
    ListModel
    {
        // common properties
        id: lmTreeContentModel
        objectName: "lmTreeContentModel"
    }
    
    /**
    * Tree item
    */
    Component
    {
        // common properties
        id: cpItem
    
        /**
        * Item column layout
        */
        Column
        {
            // common properties
            id: clItem
            objectName: "clItem"
            clip: true
    
            /**
            * Item mouse area
            */
            MouseArea
            {
                // common properties
                id: maItem
                objectName: "maItem"
                width: rwItem.implicitWidth
                height: rwItem.implicitHeight
    
                /// Called when item is double clicked
                onDoubleClicked:
                {
                    // expand/collapse item children
                    for (let i = 1; i < parent.children.length - 1; ++i)
                        parent.children[i].visible = !parent.children[i].visible;
    
                    txItemLabel.m_Text = maItem.getNodeText();
                }
    
                /**
                * Item background
                */
                Row
                {
                    // common properties
                    id: rwItem
                    objectName: "rwItem"
    
                    /**
                    * Item indent
                    */
                    Item
                    {
                        // common properties
                        width: model.level * 20
                        height: 1
                    }
    
                    /**
                    * Item label
                    */
                    Text
                    {
                        property string m_Text: maItem.getNodeText()
    
                        // common properties
                        id: txItemLabel
                        objectName: "txItemLabel"
                        text: m_Text
                        color: clItem.children.length > 2 ? "#3186ea" : "#e9d675"
                        font
                        {
                            bold: true;
                            pixelSize: 14
                        }
                    }
                }
    
                /**
                * Gets the node text
                *@return the node text
                */
                function getNodeText()
                {
                    return (clItem.children.length > 2 ?
                                    clItem.children[1].visible ?
                                            qsTr("- ") : qsTr("+ ") : qsTr("  ")) + model.name;
                }
            }
    
            /**
            * Children repeater
            */
            Repeater
            {
                // common properties
                model: childArray
                delegate: cpItem
            }
        }
    }
    
    /**
    * Tree view
    */
    ListView
    {
        // common properties
        anchors.left: parent.left
        anchors.top: rcTreeHeader.bottom
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        model: lmTreeContentModel
        delegate: cpItem
        clip: true
    
        /// Called when component is loaded
        Component.onCompleted:
        {
            for (let i = 0; i < 5; ++i)
                lmTreeContentModel.append({"name": "Node " + i, "level": 0, "childArray": []});
    
            let node = lmTreeContentModel.get(1);
    
            for (let i = 0; i < 2; ++i)
                node.childArray.append({"name": "Node 1-" + i, "level": 1, "childArray": []});
        }
    }
    
    ...
    

    This code works well and give the expected result:
    51aa13c2-95ac-48ec-8d00-901e5bd07ed5-image.png

    However, if I move the Component tree item in a separate file and if I modify the code as below:

    TreeItem.qml

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Templates 2.15 as T
    
    /**
    * Tree item
    */
    Component
    {
        // common properties
        id: cpItem
    
        /**
        * Item column layout
        */
        Column
        {
            // common properties
            id: clItem
            objectName: "clItem"
            clip: true
    
            /**
            * Item mouse area
            */
            MouseArea
            {
                // common properties
                id: maItem
                objectName: "maItem"
                width: rwItem.implicitWidth
                height: rwItem.implicitHeight
    
                /// Called when item is double clicked
                onDoubleClicked:
                {
                    //REM console.log("DOUBLE CLICKED - " + model.name + " - " + visible + " - " + parent.children.length);
    
                    // expand/collapse item children
                    for (let i = 1; i < parent.children.length - 1; ++i)
                        parent.children[i].visible = !parent.children[i].visible;
    
                    txItemLabel.m_Text = maItem.getNodeText();
                }
    
                /**
                * Item background
                */
                Row
                {
                    // common properties
                    id: rwItem
                    objectName: "rwItem"
    
                    /**
                    * Item indent
                    */
                    Item
                    {
                        // common properties
                        width: model.level * 20
                        height: 1
                    }
    
                    /**
                    * Item label
                    */
                    Text
                    {
                        property string m_Text: maItem.getNodeText()
    
                        // common properties
                        id: txItemLabel
                        objectName: "txItemLabel"
                        text: m_Text
                        color: clItem.children.length > 2 ? "#3186ea" : "#e9d675"
                        font
                        {
                            bold: true;
                            pixelSize: 14
                        }
                    }
                }
    
                /**
                * Gets the node text
                *@return the node text
                */
                function getNodeText()
                {
                    return (clItem.children.length > 2 ?
                                    clItem.children[1].visible ?
                                            qsTr("- ") : qsTr("+ ") : qsTr("  ")) + model.name;
                }
            }
    
            /**
            * Children repeater
            */
            Repeater
            {
                // common properties
                model: childArray
                delegate: cpItem
            }
        }
    }
    

    TreeView.qml:

    ...
    
    /**
    * Tree content model
    */
    ListModel
    {
        // common properties
        id: lmTreeContentModel
        objectName: "lmTreeContentModel"
    }
    
    /**
    * Tree item
    */
    TreeItem
    {
        // common properties
        id: cpItem
    }
    
    /**
    * Tree view
    */
    ListView
    {
        // common properties
        anchors.left: parent.left
        anchors.top: rcTreeHeader.bottom
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        model: lmTreeContentModel
        delegate: cpItem
        clip: true
    
        /// Called when component is loaded
        Component.onCompleted:
        {
            for (let i = 0; i < 5; ++i)
                lmTreeContentModel.append({"name": "Node " + i, "level": 0, "childArray": []});
    
            let node = lmTreeContentModel.get(1);
    
            for (let i = 0; i < 2; ++i)
                node.childArray.append({"name": "Node 1-" + i, "level": 1, "childArray": []});
        }
    }
    
    ...
    

    Then my code no longer works as expected, the children items are missing:
    244ab650-b68d-4927-93da-3c84c7ae7510-image.png

    As I barely modified the code (I only moved the Component code in another file and instanced it by TreeItem instead of directly using the cpItem identifier in the ListView delegate property), I wonder if someone may explain me why the first version is working as expected, but the second one no longer works.

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

      Have Column as the root item in TreeItem.qml, then keep define the component in TreeView.qml:

      Component {
        id: treeItemComponent
        TreeItem {
        }
      }
      

      Then it should work.

      (Z(:^

      jeanmilostJ 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Have Column as the root item in TreeItem.qml, then keep define the component in TreeView.qml:

        Component {
          id: treeItemComponent
          TreeItem {
          }
        }
        

        Then it should work.

        jeanmilostJ Offline
        jeanmilostJ Offline
        jeanmilost
        wrote on last edited by
        #3

        @sierdzio This worked for me, thank you very much. However can you explain why moving the Component itself doesn't work?

        sierdzioS 1 Reply Last reply
        0
        • jeanmilostJ jeanmilost

          @sierdzio This worked for me, thank you very much. However can you explain why moving the Component itself doesn't work?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @jeanmilost said in Why my code is no longer working when moving it in another file?:

          However can you explain why moving the Component itself doesn't work?

          I don't know, to be honest, it just felt wrong to me :D My guess would be that QML engine expects at least an Item or QtObject to be the root of a QML file. When it's presented with a component, however, the logic breaks apparently.

          (Z(:^

          J.HilkJ 1 Reply Last reply
          0
          • sierdzioS sierdzio

            @jeanmilost said in Why my code is no longer working when moving it in another file?:

            However can you explain why moving the Component itself doesn't work?

            I don't know, to be honest, it just felt wrong to me :D My guess would be that QML engine expects at least an Item or QtObject to be the root of a QML file. When it's presented with a component, however, the logic breaks apparently.

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

            @sierdzio I actually have multiple files where the root item is a Component worked always perfectly fine!


            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.

            sierdzioS 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @sierdzio I actually have multiple files where the root item is a Component worked always perfectly fine!

              sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              @J-Hilk said in Why my code is no longer working when moving it in another file?:

              @sierdzio I actually have multiple files where the root item is a Component worked always perfectly fine!

              Haha ok that's good to know :-) ... but then why my proposed fix worked here?

              (Z(:^

              J.HilkJ 1 Reply Last reply
              0
              • sierdzioS sierdzio

                @J-Hilk said in Why my code is no longer working when moving it in another file?:

                @sierdzio I actually have multiple files where the root item is a Component worked always perfectly fine!

                Haha ok that's good to know :-) ... but then why my proposed fix worked here?

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

                @sierdzio
                šŸ¤·ā€ā™‚ļø maybe copy paste error ?

                I always use

                • right click on top item
                • refactoring
                • move component into separate file

                never failed me so far :D


                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
                1

                • Login

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