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. Access item inside ListView via delegate
Forum Updated to NodeBB v4.3 + New Features

Access item inside ListView via delegate

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmllistviewdelegatebutton
14 Posts 3 Posters 4.5k 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.
  • R Offline
    R Offline
    Ratzz
    wrote on 22 Jan 2021, 04:02 last edited by Ratzz
    #1

    I was trying to access an item inside ListView via delegate in QML
    Code is similar to the link with little modification which is shown below

    alt text

    1. I want to access the TextEdit and get/set value from it .
    2. I want to hide/show button on click event ( When only one row is visible, hide the delete button )
      a) Step1: When the delete button from the row 2 is clicked
      b) Step2: Hide the delete button from row 1
    3. Also little confused on how append work. https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html#append-method . How does empty dictionary add items. Can it be empty too?

    I am using PyQt5 to launch the QML on Ubuntu 20.04, Qt 5.14.2

    Code looks like

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    Window {
        visible: true
        width: 500
        height: 500
    
        Rectangle {
            width: 250
            height: 400
            Component {
                id: listDelegate
                Item {
                    width: 250; height: 50
    
                    Row {
                        Column {
                            width: 100
                            Button { text: "text"}
                        }
                        Column {
                            width: 50
                            TextEdit { text: "Edit"}
                        }
                        Column {
                            width: 100
                            Button {
                                text: "Add"
                                onClicked:{
                                    listModel.append({"": ""})
                                }
                            }
                        }
                        Column {
                            width: 100
                            Button {
                                text: "Delete"
                                onClicked:{
                                    if(listModel.count > 1)
                                        listModel.remove(index);
                                }
                            }
                        }
                    }
                }
            }
    
            ListModel {
                id: listModel
                ListElement {
                }
            }
    
            ListView {
                id: listView
                anchors.fill: parent
                model: listModel
                delegate: listDelegate
                focus: true
            }
        }
    }
    
    

    --Alles ist gut.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Ratzz
      wrote on 25 Jan 2021, 05:23 last edited by Ratzz
      #2

      I was able to access the TextEdit value using property value which is set to model via ListElement something like this

      ListElement { editProperty: "Initial text" }
      

      and on click event

       onClicked: { listModel.setProperty(index, "editProperty", editField.text) }
      
      

      Still I am unable to access the delete Button . Does anyone know to access it?

      --Alles ist gut.

      J R 2 Replies Last reply 25 Jan 2021, 06:38
      0
      • R Ratzz
        25 Jan 2021, 05:23

        I was able to access the TextEdit value using property value which is set to model via ListElement something like this

        ListElement { editProperty: "Initial text" }
        

        and on click event

         onClicked: { listModel.setProperty(index, "editProperty", editField.text) }
        
        

        Still I am unable to access the delete Button . Does anyone know to access it?

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 25 Jan 2021, 06:38 last edited by
        #3

        hi @Ratzz

        you already "listen" to the listModel count property for your onClicked why don't you make the visible property depending on it as well ?

                          Column {
                                width: 100
                                Button {
                                    text: "Delete"
                                    visible: listModel.count > 1
                                    onClicked:{
                                        if(listModel.count > 1)
                                            listModel.remove(index);
                                    }
                                }
                            }
        

        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.

        R 1 Reply Last reply 25 Jan 2021, 07:03
        2
        • J J.Hilk
          25 Jan 2021, 06:38

          hi @Ratzz

          you already "listen" to the listModel count property for your onClicked why don't you make the visible property depending on it as well ?

                            Column {
                                  width: 100
                                  Button {
                                      text: "Delete"
                                      visible: listModel.count > 1
                                      onClicked:{
                                          if(listModel.count > 1)
                                              listModel.remove(index);
                                      }
                                  }
                              }
          
          R Offline
          R Offline
          Ratzz
          wrote on 25 Jan 2021, 07:03 last edited by
          #4

          @J-Hilk said in Access item inside ListView via delegate:

          you already "listen" to the listModel count property for your onClicked why don't you make the visible property depending on it as well ?

          It just worked :) Thank you @J-Hilk

          --Alles ist gut.

          1 Reply Last reply
          1
          • R Ratzz
            25 Jan 2021, 05:23

            I was able to access the TextEdit value using property value which is set to model via ListElement something like this

            ListElement { editProperty: "Initial text" }
            

            and on click event

             onClicked: { listModel.setProperty(index, "editProperty", editField.text) }
            
            

            Still I am unable to access the delete Button . Does anyone know to access it?

            R Offline
            R Offline
            Ratzz
            wrote on 25 Jan 2021, 07:05 last edited by
            #5

            @Ratzz said in Access item inside ListView via delegate:

            I was able to access the TextEdit value using property value which is set to model via ListElement something like this

            @J-Hilk , Is there any better way than this ?

            --Alles ist gut.

            J 1 Reply Last reply 25 Jan 2021, 07:11
            0
            • R Ratzz
              25 Jan 2021, 07:05

              @Ratzz said in Access item inside ListView via delegate:

              I was able to access the TextEdit value using property value which is set to model via ListElement something like this

              @J-Hilk , Is there any better way than this ?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 25 Jan 2021, 07:11 last edited by
              #6

              @Ratzz
              for example, you could just bind the text directly:

                                  Column {
                                      width: 50
                                      TextEdit { text: editableText}
                                  }
              ...
              ...
                   ListModel {
                          id: listModel
                          ListElement {
                                  editableText: "SomeText"
                          }
                   }
              

              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.

              R 1 Reply Last reply 25 Jan 2021, 12:04
              2
              • R Offline
                R Offline
                Ratzz
                wrote on 25 Jan 2021, 07:18 last edited by
                #7

                That works as well

                --Alles ist gut.

                1 Reply Last reply
                0
                • J J.Hilk
                  25 Jan 2021, 07:11

                  @Ratzz
                  for example, you could just bind the text directly:

                                      Column {
                                          width: 50
                                          TextEdit { text: editableText}
                                      }
                  ...
                  ...
                       ListModel {
                              id: listModel
                              ListElement {
                                      editableText: "SomeText"
                              }
                       }
                  
                  R Offline
                  R Offline
                  Ratzz
                  wrote on 25 Jan 2021, 12:04 last edited by Ratzz
                  #8

                  @J-Hilk
                  Now when I tried with empty text

                  ListModel {
                              id: listModel
                              ListElement {
                                      editableText: ""
                              }
                       }
                  

                  and then I tried to change the text field value manually say "sometext" . Button event returned null

                  onClicked: { 
                  listModel.get(0).editableText 
                  console.log(listModel.get(0).editableText ) //Null
                  }
                  

                  while button event setting property gave proper result

                  onClicked: { 
                  listModel.setProperty(index, "editableText ", id.text)
                  console.log(listModel.get(0).editableText ) //"sometext"
                  }

                  --Alles ist gut.

                  J GrecKoG 2 Replies Last reply 25 Jan 2021, 12:26
                  0
                  • R Ratzz
                    25 Jan 2021, 12:04

                    @J-Hilk
                    Now when I tried with empty text

                    ListModel {
                                id: listModel
                                ListElement {
                                        editableText: ""
                                }
                         }
                    

                    and then I tried to change the text field value manually say "sometext" . Button event returned null

                    onClicked: { 
                    listModel.get(0).editableText 
                    console.log(listModel.get(0).editableText ) //Null
                    }
                    

                    while button event setting property gave proper result

                    onClicked: { 
                    listModel.setProperty(index, "editableText ", id.text)
                    console.log(listModel.get(0).editableText ) //"sometext"
                    }
                    J Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 25 Jan 2021, 12:26 last edited by
                    #9

                    @Ratzz interesting

                    I usually have c++ based models, so my experience with QML models is limited 😕

                    Maybe someone else know more about this behavior


                    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.

                    R 1 Reply Last reply 25 Jan 2021, 13:35
                    1
                    • J J.Hilk
                      25 Jan 2021, 12:26

                      @Ratzz interesting

                      I usually have c++ based models, so my experience with QML models is limited 😕

                      Maybe someone else know more about this behavior

                      R Offline
                      R Offline
                      Ratzz
                      wrote on 25 Jan 2021, 13:35 last edited by Ratzz
                      #10

                      @J-Hilk
                      okay Fine.

                      --Alles ist gut.

                      1 Reply Last reply
                      0
                      • R Ratzz
                        25 Jan 2021, 12:04

                        @J-Hilk
                        Now when I tried with empty text

                        ListModel {
                                    id: listModel
                                    ListElement {
                                            editableText: ""
                                    }
                             }
                        

                        and then I tried to change the text field value manually say "sometext" . Button event returned null

                        onClicked: { 
                        listModel.get(0).editableText 
                        console.log(listModel.get(0).editableText ) //Null
                        }
                        

                        while button event setting property gave proper result

                        onClicked: { 
                        listModel.setProperty(index, "editableText ", id.text)
                        console.log(listModel.get(0).editableText ) //"sometext"
                        }
                        GrecKoG Offline
                        GrecKoG Offline
                        GrecKo
                        Qt Champions 2018
                        wrote on 25 Jan 2021, 15:45 last edited by
                        #11

                        @Ratzz said in Access item inside ListView via delegate:

                        listModel.get(0).editableText

                        What is that line supposed to do? You are not modifying anything here

                        onClicked: { 
                            listModel.setProperty(index, "editableText ", textEdit.text)
                        }
                        

                        just do that instead:

                        onClicked: editableText = textEdit.text
                        
                        1 Reply Last reply
                        1
                        • R Offline
                          R Offline
                          Ratzz
                          wrote on 25 Jan 2021, 15:52 last edited by
                          #12

                          @GrecKo , Yes I can do that as well.

                          How can I get the updated text "editableText" as soon I change the value of TextEdit is changed?

                          --Alles ist gut.

                          GrecKoG 1 Reply Last reply 25 Jan 2021, 15:56
                          0
                          • R Ratzz
                            25 Jan 2021, 15:52

                            @GrecKo , Yes I can do that as well.

                            How can I get the updated text "editableText" as soon I change the value of TextEdit is changed?

                            GrecKoG Offline
                            GrecKoG Offline
                            GrecKo
                            Qt Champions 2018
                            wrote on 25 Jan 2021, 15:56 last edited by
                            #13

                            @Ratzz console.log(editableText ), you have access to the role values (as context properties) corresponding to the model cell in the delegate

                            1 Reply Last reply
                            0
                            • R Offline
                              R Offline
                              Ratzz
                              wrote on 25 Jan 2021, 15:59 last edited by Ratzz
                              #14

                              @GrecKo , I mean without the button, when I edit the values of TextEdit, I should be able to get the modified text. I tried via property. It always gave me empty

                              --Alles ist gut.

                              1 Reply Last reply
                              0

                              2/14

                              25 Jan 2021, 05:23

                              topic:navigator.unread, 12
                              • Login

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