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 to extend qml containers (Row or Column) in another file
Forum Updated to NodeBB v4.3 + New Features

How to extend qml containers (Row or Column) in another file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
rowqtquick
4 Posts 4 Posters 656 Views 2 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.
  • A Offline
    A Offline
    alizadeh91
    wrote on last edited by
    #1

    Suppose there is a Test.qml file containing this:

    import QtQuick 2.0
    
    Rectangle {
        color: "green"
    
        Row {
            id: row
            spacing: 10
            anchors.fill: parent
            Rectangle {
                color: "red";
                width: 100;
                height: 100;
            }
            Rectangle {
                color: "red";
                width: 100;
                height: 100;
            }
            Rectangle {
                color: "red";
                width: 100;
                height: 100;
            }
        }
    }
    

    Now suppose we want to use this Test.qml within another file like main.qml:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    
    Window {
        id: window
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
    
        Test {
            anchors.fill: parent;
            // I want to be able to add new items (rects) to the row inside Test.qml
        }
    }
    

    Now suppose we want to extend items to the row object in Test.qml, But we want to add from main.qml. How we can do that? is that even possible?

    (FYI: The application of this feature would be to develop a placeholder form and fill the items in the other items so we can skip duplicate codes. )

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

      https://together.jolla.com/question/4717/how-to-position-nested-items-in-a-qml-component/

      Item {
          default property alias _contentChildren: content.data 
      
          Item {
              id: content
              // where you want the new Items to appear
          }
      }
      

      I have no idea where this is documented. I can never remember what it is called. Had to look at my code to find it.

      Another option is an alias to content.data to another name:

      Item {
          property alias delegate: content.data
      
          Item {
              id: content
          }
      }
      

      Then use delegate to hold new items.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #2

        Expose the id: row as alias property
        property alias rowPos : row

        For new QML objects set the row as parent.
        Alternatively you can declare one method inside Test.qml
        function addNewItem(newitem){
        newItem.parent = row.
        }

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

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

          https://together.jolla.com/question/4717/how-to-position-nested-items-in-a-qml-component/

          Item {
              default property alias _contentChildren: content.data 
          
              Item {
                  id: content
                  // where you want the new Items to appear
              }
          }
          

          I have no idea where this is documented. I can never remember what it is called. Had to look at my code to find it.

          Another option is an alias to content.data to another name:

          Item {
              property alias delegate: content.data
          
              Item {
                  id: content
              }
          }
          

          Then use delegate to hold new items.

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          1
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #4

            It's documented in the QML Reference : https://doc.qt.io/qt-5/qtqml-syntax-objectattributes.html#property-attributes

            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