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

  • 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
                name: "D1"
                name: "P1"
                name: "W1"
        Component {
            id: configDelegate
            Item {
                width: parent.width
                height: 30
                    Text {
                        font.pixelSize: 20
                        verticalAlignment: Text.AlignBottom
                        horizontalAlignment: Text.AlignLeft
                        color: "white"
                        text: name
                MouseArea {
                    anchors.fill: parent
                            config_list.currentIndex = index

    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.

                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 ?

  • @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.

  • Qt Champions 2017

    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.

  • 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?

  • 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.

    This is another example in which I use the same custom model to display the content in a QML TreeView.

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.