Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Not able to override delegate model, index

Not able to override delegate model, index

Scheduled Pinned Locked Moved Unsolved Qt 6
5 Posts 3 Posters 1.0k 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.
  • Ash VA Offline
    Ash VA Offline
    Ash V
    wrote on last edited by Ash V
    #1

    @Axel-Spoerl, @JonB, @Christian-Ehrlicher
    Hi there!

    I have two files in my simple project, main.qml, and Delegate.qml:

    // main.qml
    
    import QtQuick
    import content
    
    App {
        height: 750
        width: 375
        visible: true
    
        ListView {
            id: lv
    
            anchors.fill: parent
            model: ListModel {
                ListElement {
                    name: "Hebrew"
                    type: "language"
                }
                ListElement {
                    name: "Aramaic"
                    type: "language"
                }
            }
            delegate: Delegate {}
        }
    }
    
    // Delegate.qml
    
    import QtQuick
    
    Rectangle {
        id: del
    
        height: 50
        width: 250
    
        property var model: ({})
        property int index: -1
    
        Text {
            text: {
                index + ", " + model.name + ", " + model.type
            }
        }
    }
    

    The output I'm getting is this:

    However, when I changed my Delegate.qml as follows:

    // Delegate.qml
    
    import QtQuick
    
    Rectangle {
        id: del
    
        height: 50
        width: 250
    
        required property var model
        required property var index
    
        Text {
            text: {
                index + ", " + model.name + ", " + model.type
            }
        }
    }
    

    or even to this:

    import QtQuick
    
    Rectangle {
        id: del
    
        height: 50
        width: 250
    
        Text {
            text: {
                index + ", " + model.name + ", " + model.type
            }
        }
    }
    

    I get the desired result:

    Could you please explain to me why the model and index properties are not being overridden in the delegate?

    Is there a way to override them, as I intend to do it here? Please explain.

    Axel SpoerlA 1 Reply Last reply
    0
    • Ash VA Ash V

      @Axel-Spoerl, @JonB, @Christian-Ehrlicher
      Hi there!

      I have two files in my simple project, main.qml, and Delegate.qml:

      // main.qml
      
      import QtQuick
      import content
      
      App {
          height: 750
          width: 375
          visible: true
      
          ListView {
              id: lv
      
              anchors.fill: parent
              model: ListModel {
                  ListElement {
                      name: "Hebrew"
                      type: "language"
                  }
                  ListElement {
                      name: "Aramaic"
                      type: "language"
                  }
              }
              delegate: Delegate {}
          }
      }
      
      // Delegate.qml
      
      import QtQuick
      
      Rectangle {
          id: del
      
          height: 50
          width: 250
      
          property var model: ({})
          property int index: -1
      
          Text {
              text: {
                  index + ", " + model.name + ", " + model.type
              }
          }
      }
      

      The output I'm getting is this:

      However, when I changed my Delegate.qml as follows:

      // Delegate.qml
      
      import QtQuick
      
      Rectangle {
          id: del
      
          height: 50
          width: 250
      
          required property var model
          required property var index
      
          Text {
              text: {
                  index + ", " + model.name + ", " + model.type
              }
          }
      }
      

      or even to this:

      import QtQuick
      
      Rectangle {
          id: del
      
          height: 50
          width: 250
      
          Text {
              text: {
                  index + ", " + model.name + ", " + model.type
              }
          }
      }
      

      I get the desired result:

      Could you please explain to me why the model and index properties are not being overridden in the delegate?

      Is there a way to override them, as I intend to do it here? Please explain.

      Axel SpoerlA Offline
      Axel SpoerlA Offline
      Axel Spoerl
      Moderators
      wrote on last edited by
      #2

      @Ash-V
      Maybe I get this wrong, but in the first version, the properties are actually overridden. You can see that at the index being -1.
      In the second version, bindings are created and you see the desired output. So in my view, all version behave exactly as expected.

      Software Engineer
      The Qt Company, Oslo

      Ash VA 1 Reply Last reply
      1
      • Axel SpoerlA Axel Spoerl

        @Ash-V
        Maybe I get this wrong, but in the first version, the properties are actually overridden. You can see that at the index being -1.
        In the second version, bindings are created and you see the desired output. So in my view, all version behave exactly as expected.

        Ash VA Offline
        Ash VA Offline
        Ash V
        wrote on last edited by Ash V
        #3

        @Axel-Spoerl
        Hi Axel!

        What you've said doesn't seem quite correct, as when I initialized the property index with -5, I got -5 in my output.

        // Delegate.qml
        
        import QtQuick
        
        Rectangle {
            id: del
        
            height: 50
            width: 250
        
            property var model: ({})
            property int index: -5
        
            Text {
                text: {
                    index + ", " + model.name + ", " + model.type
                }
            }
        }
        


        This simply means that the model and index properties are not being overridden, isn't it?

        Ping @JonB, @Christian-Ehrlicher

        JonBJ Axel SpoerlA 2 Replies Last reply
        0
        • Ash VA Ash V

          @Axel-Spoerl
          Hi Axel!

          What you've said doesn't seem quite correct, as when I initialized the property index with -5, I got -5 in my output.

          // Delegate.qml
          
          import QtQuick
          
          Rectangle {
              id: del
          
              height: 50
              width: 250
          
              property var model: ({})
              property int index: -5
          
              Text {
                  text: {
                      index + ", " + model.name + ", " + model.type
                  }
              }
          }
          


          This simply means that the model and index properties are not being overridden, isn't it?

          Ping @JonB, @Christian-Ehrlicher

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Ash-V
          Please don't "ping" people or address them in questions. We all see new questions, and we answer what we can. For my part I have never used QML so I'm hardly going to know about this issue.

          1 Reply Last reply
          0
          • Ash VA Ash V

            @Axel-Spoerl
            Hi Axel!

            What you've said doesn't seem quite correct, as when I initialized the property index with -5, I got -5 in my output.

            // Delegate.qml
            
            import QtQuick
            
            Rectangle {
                id: del
            
                height: 50
                width: 250
            
                property var model: ({})
                property int index: -5
            
                Text {
                    text: {
                        index + ", " + model.name + ", " + model.type
                    }
                }
            }
            


            This simply means that the model and index properties are not being overridden, isn't it?

            Ping @JonB, @Christian-Ehrlicher

            Axel SpoerlA Offline
            Axel SpoerlA Offline
            Axel Spoerl
            Moderators
            wrote on last edited by
            #5

            @Ash-V
            Hi,
            index is set to -5, model is set to {}, which means it's an empty, default constructed model.
            Regardless if overridden or not, modelneither has a name, nor a typeproperty.
            So I don't quite see the problem here.
            Which value would you expect the properties to have?

            Software Engineer
            The Qt Company, Oslo

            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