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. Reading value from ListView item, after changing it's model
Forum Updated to NodeBB v4.3 + New Features

Reading value from ListView item, after changing it's model

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 791 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.
  • K Offline
    K Offline
    krokstr
    wrote on last edited by
    #1

    Hello,

    I have a ListView which I want to fill with data from C++'s QStringList.

            ListView {
                id: config_list
                anchors.fill: parent
                model: config_model
                delegate: configDelegate
                clip: true
                highlightFollowsCurrentItem: true
                highlight: Rectangle {
                    color: 'grey'
                    radius: 5
                }
            }
    ListModel{
            id:config_model
            ListElement{
                name: "D1"
            }
            ListElement{
                name: "P1"
            }
            ListElement{
                name: "W1"
            }
        }
        Component {
            id: configDelegate
            Item {
                width: parent.width
                height: 30
                Row{
                    Text {
                        font.pixelSize: 20
                        verticalAlignment: Text.AlignBottom
                        horizontalAlignment: Text.AlignLeft
                        color: "white"
                        text: name
                    }
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked:{
                            config_list.currentIndex = index
                        console.log(qsTr(config_model.get(config_list.currentIndex).name))
                    }
                }
            }
        }
    

    So I do have the next in my .cpp file :

    Q_PROPERTY(QStringList model MEMBER m_model NOTIFY modelChanged) 
    

    I'm filling the list with the data wanted and reading it from QML.

    button{
            onClicked:{
                conf_page.config_model.clear()
                myClass.onReadModel()
                conf_page.config_list.model = myClass.model
            }
        }
    

    But when the new model is loaded, the data doesn't show up until i change the value of configDelegate's text.
    text: modelData,
    instead of
    text: name.

    Now I can see the text in the ListView, but I can not read the data with current method i'm using (console.log(qsTr(config_model.get(config_list.currentIndex).name))).
    I've tried different approaches, but couldn't manage to fix it.

    How should I do it ?

    Gojir4G 1 Reply Last reply
    0
    • K krokstr

      Hello,

      I have a ListView which I want to fill with data from C++'s QStringList.

              ListView {
                  id: config_list
                  anchors.fill: parent
                  model: config_model
                  delegate: configDelegate
                  clip: true
                  highlightFollowsCurrentItem: true
                  highlight: Rectangle {
                      color: 'grey'
                      radius: 5
                  }
              }
      ListModel{
              id:config_model
              ListElement{
                  name: "D1"
              }
              ListElement{
                  name: "P1"
              }
              ListElement{
                  name: "W1"
              }
          }
          Component {
              id: configDelegate
              Item {
                  width: parent.width
                  height: 30
                  Row{
                      Text {
                          font.pixelSize: 20
                          verticalAlignment: Text.AlignBottom
                          horizontalAlignment: Text.AlignLeft
                          color: "white"
                          text: name
                      }
                  }
                  MouseArea {
                      anchors.fill: parent
                      onClicked:{
                              config_list.currentIndex = index
                          console.log(qsTr(config_model.get(config_list.currentIndex).name))
                      }
                  }
              }
          }
      

      So I do have the next in my .cpp file :

      Q_PROPERTY(QStringList model MEMBER m_model NOTIFY modelChanged) 
      

      I'm filling the list with the data wanted and reading it from QML.

      button{
              onClicked:{
                  conf_page.config_model.clear()
                  myClass.onReadModel()
                  conf_page.config_list.model = myClass.model
              }
          }
      

      But when the new model is loaded, the data doesn't show up until i change the value of configDelegate's text.
      text: modelData,
      instead of
      text: name.

      Now I can see the text in the ListView, but I can not read the data with current method i'm using (console.log(qsTr(config_model.get(config_list.currentIndex).name))).
      I've tried different approaches, but couldn't manage to fix it.

      How should I do it ?

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @krokstr Hi,

      get() is specific to QML type ListModel but doesn't exist in QStringListModel. So you cannot call it with a C++ model, except if you make your own model and implement the get method.

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

        I felt some cleanup is required in your program. First you are creating the model in QML. You are creating the StringList and exposing it as model. QStringList is not a model. QStringListModel is model. I saw some data is fetched from C++ and trying to assign the model in QML. I'm not very clear why all this round about.

        Either you can create the model in QML & stay with it. If not create the model in C++ and expose this model itself to QML using setContextProperty. You can directly assign C++ model to ListView. This will simplify your program.

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          krokstr
          wrote on last edited by
          #4

          Okay, I'm fairly new to QML and I'm sorry if I'm asking stupid questions!
          I've tried some new methods but, I can't understand you very well. I couldn't make it with changing to QStringListModel.
          So, what is the best way to fill up the ListView from C++, preferably preserving the current ListModel with "name" parameter in it . I am using C++ for searching some files in a directory, put their names in QStringList, then I want to transfer the data to ListView in QML. Can you send me some snippet code and links to something that can help me?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mammamia
            wrote on last edited by
            #5

            Hi @krokstr , If you want to show files in a folder you can also try using QFileSystemModel and show the file names in the QML view.

            Below is my blog post in which I create a custom FileSystemModel which fetch the directory information and show on a TreeeWidget.
            https://arunpkqt.wordpress.com/2017/01/05/custom-filesystemmodel-display/

            This is another example in which I use the same custom model to display the content in a QML TreeView.
            https://github.com/arunpkqt/QMLTreeView

            You can refer those two. Hope it helps. Happy coding!

            1 Reply Last reply
            0

            • Login

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