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. ListView of custom Component
Forum Updated to NodeBB v4.3 + New Features

ListView of custom Component

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.1k Views 1 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.
  • D Offline
    D Offline
    DavidM29
    wrote on 18 Sept 2018, 10:21 last edited by
    #1

    Hello,

    I'm trying to make a list view of a custom component.
    This component is called ManualFunction.qml.
    It is made of Rectangles, image and buttons.
    I would like a list of this component where I change a few properties.

    The properties I want to change are the text on 2 buttons, and the source of the image.
    The properties are define like that in the ManualFunction.qml :

        property string textButtonR: ""
        property string textButtonL: ""
        property string imageSource: ""
    

    Here is what I tried to create the listView:

    
            ListModel {
                id: myModel
                ListElement {
                    textButtonR: "Open"
                    textButtonL: "Close"
                    imageSource: "../img/sun.svg"
                }
    
                ListElement {
                    textButtonR: "Open"
                    textButtonL: "Close"
                    imageSource: "../img/sun.svg"
                }
    
                ListElement {
                    textButtonR: "Open"
                    textButtonL: "Close"
                    imageSource: "../img/sun.svg"
                }
    
            }
            
            Component{
                id: del
                ManualFunction{
                    
                }
            }
    
            ListView{
                anchors.fill: parent
                anchors.horizontalCenter: parent.horizontalCenter
                model: myModel
                delegate: del
            }
    

    But I only get one ManualFunction without sources and Text in button. I may have misunderstood the way ListView works.
    Can you please help me make this works ?

    G 1 Reply Last reply 18 Sept 2018, 10:37
    0
    • D DavidM29
      18 Sept 2018, 10:21

      Hello,

      I'm trying to make a list view of a custom component.
      This component is called ManualFunction.qml.
      It is made of Rectangles, image and buttons.
      I would like a list of this component where I change a few properties.

      The properties I want to change are the text on 2 buttons, and the source of the image.
      The properties are define like that in the ManualFunction.qml :

          property string textButtonR: ""
          property string textButtonL: ""
          property string imageSource: ""
      

      Here is what I tried to create the listView:

      
              ListModel {
                  id: myModel
                  ListElement {
                      textButtonR: "Open"
                      textButtonL: "Close"
                      imageSource: "../img/sun.svg"
                  }
      
                  ListElement {
                      textButtonR: "Open"
                      textButtonL: "Close"
                      imageSource: "../img/sun.svg"
                  }
      
                  ListElement {
                      textButtonR: "Open"
                      textButtonL: "Close"
                      imageSource: "../img/sun.svg"
                  }
      
              }
              
              Component{
                  id: del
                  ManualFunction{
                      
                  }
              }
      
              ListView{
                  anchors.fill: parent
                  anchors.horizontalCenter: parent.horizontalCenter
                  model: myModel
                  delegate: del
              }
      

      But I only get one ManualFunction without sources and Text in button. I may have misunderstood the way ListView works.
      Can you please help me make this works ?

      G Offline
      G Offline
      Gojir4
      wrote on 18 Sept 2018, 10:37 last edited by
      #2

      @DavidM29 Hi,

      You need to bind value from model to your delegate.

       Component{
                  id: del
                  ManualFunction{
                      textButtonR: model.textButtonR
                      textButtonL: model.textButtonL
                      imageSource: model.imageSource
                  }
              }
      
      1 Reply Last reply
      2
      • D Offline
        D Offline
        DavidM29
        wrote on 18 Sept 2018, 10:43 last edited by DavidM29
        #3

        @Gojir4 Thank you,
        It kind of works. I have now one element with the data I want in the button and image. But why do I have only one element ? I put three list element in my ListModel.
        Could they possibly be on top of the other ?

        O 1 Reply Last reply 18 Sept 2018, 10:52
        0
        • D DavidM29
          18 Sept 2018, 10:43

          @Gojir4 Thank you,
          It kind of works. I have now one element with the data I want in the button and image. But why do I have only one element ? I put three list element in my ListModel.
          Could they possibly be on top of the other ?

          O Offline
          O Offline
          ODБOï
          wrote on 18 Sept 2018, 10:52 last edited by ODБOï
          #4

          @DavidM29 hi,

          for a ManualFunction.qml defined like this;
          //ManualFunction.qml

          import QtQuick 2.0
          import QtQuick.Controls 2.2
          Rectangle{
              id: r
              height: 90
              width: 200
              color:'indigo'
              property string textButtonR: ""
              property string imageSource: ""
              Column{
                  anchors.fill: parent
                  Image{
                      source:imageSource
                      height: 20
                      width: 20
                  }
                  Text{
                      text:textButtonR
                  }
              }
          }
          
          

          //main.qml

          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              ListModel {
                          id: myModel
                          ListElement {
                              btntxt: "Open"
                              textButtonL: "Close"
                              imgsrc: "spin.png"
          
          
                          }
          
                          ListElement {
                              btntxt: "Open"
                              textButtonL: "Close"
                              imgsrc: "spin.png"
          
          
                          }
          
                          ListElement {
                              btntxt: "Open"
                              textButtonL: "Close"
                              imgsrc: "spin.png"
          
                          }
          
                      }
           ListView{
              anchors.fill: parent
              clip: true
              model:myModel
              delegate:ManualFunction{
              imageSource: imgsrc
              textButtonR: btntxt + " " +  index
             }
           }
          }
          
          1 Reply Last reply
          2
          • D Offline
            D Offline
            DavidM29
            wrote on 18 Sept 2018, 11:44 last edited by
            #5

            Thank you ! The list element where on top of eachother. I just modified the ManualFunction.qml to correct this by setting a height.

            1 Reply Last reply
            1

            1/5

            18 Sept 2018, 10:21

            • Login

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