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. How to hook QStringListModel up to a ListView?
Forum Updated to NodeBB v4.3 + New Features

How to hook QStringListModel up to a ListView?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 4 Posters 968 Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to devnet,

    At the bottom of the QtQuick model view documentation, you can find a link to the QtQuick CPP models documentation that explains how to implement interaction between both.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    J 1 Reply Last reply
    1
    • SGaistS SGaist

      Hi and welcome to devnet,

      At the bottom of the QtQuick model view documentation, you can find a link to the QtQuick CPP models documentation that explains how to implement interaction between both.

      J Offline
      J Offline
      Jay_emissary
      wrote on last edited by
      #3

      @SGaist Hey SGaist, thank you for the quick response and resources! I'm still trying to wrap my head around the whole model thing with Qt, so forgive me if I have a few "obvious" questions. I've noticed how in the documentation they have the required property string "modelData" is it required to have that variable initialized in order to use modelData? I swear I've seen it used without even having the property in code. In my most recent build, when I remove the property, I get "undefined" when I log out modelData.

      Another problem I have is even though the modelData property no longer comes up as "undefined" (due to "required property string modelData" being added) in my code, it logs " " in the console.log function. And I know that the list isn't empty as right before I run console.log, I print the QList in c++ and I currently have 1 item: QList("TargetDirectories.txt")

      ListView{
                        id:fileinfo
                        model: FileDataHandler.files
      
                        Layout.topMargin: 10
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        spacing: 10
                        delegate: Flow{
      
                            required property string modelData
                            anchors.left: parent.left
                            anchors.right: parent.right
                            anchors.leftMargin: 20
                            anchors.rightMargin: 20
                            spacing: 10
                            width: fileinfo.width
      
                            ColumnLayout{
                                Layout.alignment: Qt.AlignHCenter
                                Layout.preferredWidth: window.width * .2
                                Layout.preferredHeight: window.height * .1
                                Image {
                                    Layout.alignment: Qt.AlignHCenter
                                    id: myImage
                                    source: "Icons/document.svg"
                                    fillMode: Image.PreserveAspectFit
                                    sourceSize: Qt.size(24,24)
                                    Layout.preferredWidth: window.width * .1
                                    Layout.preferredHeight: window.height * .05
                                }
      
      
                                TextField{
      
                                    color: "#000000"
                                    Layout.alignment: Qt.AlignHCenter
                                    text:   modelData //this is now valid
      
                                    onTextChanged: function(){
                                        console.log(modelData, "MODEL")
                                    }
      
      
                                    background: Rectangle{
                                        radius: 30
                                    }
                                    clip: true
      
                                }
                            }
      
      
                        }
                    }
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        You should provide the C++ part as well so we have a better picture of what you are doing.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        0
        • SGaistS SGaist

          You should provide the C++ part as well so we have a better picture of what you are doing.

          J Offline
          J Offline
          Jay_emissary
          wrote on last edited by
          #5

          @SGaist
          The code is being marked as spam. What should I do?

          J 1 Reply Last reply
          0
          • J Jay_emissary

            @SGaist
            The code is being marked as spam. What should I do?

            J Offline
            J Offline
            Jay_emissary
            wrote on last edited by
            #6

            @Jay_emissary I have an idea. 6325cd7d-b018-46fe-af42-9ba15c15b72f-image.png

            #include "filedatahandler.h"
            #include <QFIleDialog>
            #include <QDir>
            #include <QStandardPaths>
            #include <QFileSystemModel>
            
            
            
            FileDataHandler::FileDataHandler(QObject * parent) : QFileSystemModel(parent) {
            
                //qurls return an empty string when converted into a local file without file:///
                //QStandardPaths doesn't return a string in that format, so we handle this manually
                defaultPath = "file:///" + (QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
            
                filters << "*.txt";
                setNameFilters(filters);
                setRootDirectory(defaultPath);
            }
            
            
            void FileDataHandler::setRootDirectory( QUrl selectedFolderPath)
            {
                //convert url into a qstring that's compatible with setRootPath()
                QString selectedPath = selectedFolderPath.toLocalFile();
                if (QDir(selectedPath).exists()){
                    setRootPath(selectedPath);
                    setRootIndex(index(selectedPath));
                    if(m_files){
                        m_files->setStringList( QDir(selectedPath).entryList(filters, QDir::Files));
                        setFiles(m_files);
                        qInfo() << m_files->stringList();
                    }
                    else
                    {
                        qWarning() << "m_files is invalid";
                    }
            
                    qInfo() << rootPath()<< selectedPath;
                    return;
                }
                else{
                    qWarning() << "Cannot set a default path.";
            
                }
            
            }
            
            QString FileDataHandler::getDefaultRootDirectory()
            {
                //returns documents directory as string (note that writeableLocation is used if you're just reading or writing)
                return QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
            }
            
            QModelIndex FileDataHandler::rootIndex() const
            {
                return m_rootIndex;
            }
            
            void FileDataHandler::setRootIndex(const QModelIndex &newRootIndex)
            {
                if (m_rootIndex == newRootIndex)
                    return;
                m_rootIndex = newRootIndex;
                emit rootIndexChanged();
            }
            
            QStringListModel *FileDataHandler::files() const
            {
                return m_files;
            }
            
            void FileDataHandler::setFiles(QStringListModel *newFiles)
            {
                if (m_files == newFiles)
                    return;
                m_files = newFiles;
                emit filesChanged();
            }
            
            
            1 Reply Last reply
            0
            • Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #7

              The QML_ELEMENT macro is missing.
              Have you read the documentation about writing QML extensions?

              Software Engineer
              The Qt Company, Oslo

              J 1 Reply Last reply
              0
              • Axel SpoerlA Axel Spoerl

                The QML_ELEMENT macro is missing.
                Have you read the documentation about writing QML extensions?

                J Offline
                J Offline
                Jay_emissary
                wrote on last edited by
                #8

                @Axel-Spoerl I admit that I haven't read this part of the document yet. I've been using youtube tutorials to help me understand the QML_ELEMENT macro for the most part. To my understanding, you don't need the macro if you register your class via qmlRegisterSingletonInstance, No?

                Axel SpoerlA 1 Reply Last reply
                0
                • J Jay_emissary

                  @Axel-Spoerl I admit that I haven't read this part of the document yet. I've been using youtube tutorials to help me understand the QML_ELEMENT macro for the most part. To my understanding, you don't need the macro if you register your class via qmlRegisterSingletonInstance, No?

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

                  Hm, you haven't said that you are implementing the model as a singleton.
                  There is nothing in the code, that worries me particularly.
                  I suggest you boil this down to a minimal reproducible example, to narrow the problem down to the smallest possible amount of code.
                  Start with a model that isn't a singleton and see if it works.

                  Software Engineer
                  The Qt Company, Oslo

                  J 1 Reply Last reply
                  0
                  • Axel SpoerlA Axel Spoerl

                    Hm, you haven't said that you are implementing the model as a singleton.
                    There is nothing in the code, that worries me particularly.
                    I suggest you boil this down to a minimal reproducible example, to narrow the problem down to the smallest possible amount of code.
                    Start with a model that isn't a singleton and see if it works.

                    J Offline
                    J Offline
                    Jay_emissary
                    wrote on last edited by Jay_emissary
                    #10

                    @Axel-Spoerl Hey so, I went ahead and made an example project, with minimal code. I created the type, initialized the string list in the constructor, and sent the data to the model. Strangely enough, nothing appears on the text and when I trigger the console.log function, I get
                    "qml: Model Data: undefined"
                    902f7a72-4d45-4e19-8192-d9948b8ba978-image.png
                    a2ea171d-fdcb-4bd9-a2dc-a546207b01fa-image.png
                    27b73bde-e3ff-49d9-a6f0-a5595ecffe38-image.png
                    2f814729-b1a3-46ec-ac33-e2d57ce05fa0-image.png

                    81143b73-b99c-4ce5-b687-50f440efd50c-image.png

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote on last edited by
                      #11

                      You registered a type and used it as a singleton in QML. Also avoid using qmlRegister*** function now, you should use QML_xxx macros instead.

                      J 1 Reply Last reply
                      0
                      • GrecKoG GrecKo

                        You registered a type and used it as a singleton in QML. Also avoid using qmlRegister*** function now, you should use QML_xxx macros instead.

                        J Offline
                        J Offline
                        Jay_emissary
                        wrote on last edited by
                        #12

                        @GrecKo In my original project, the object with the StringListModel is a singleton. In this new one, the StringListModelExampleClass isn't. Anyway, I had no idea that using macros is now considered "best practice" for communicating with qml, so thank you for the advice! My project is now using the QML_ELEMENT macro, but the console is still logging "undefined."
                        34665fef-3729-4691-933c-63db6a2b872b-image.png

                        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