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. ListView model from Q_PROPERTY problems
QtWS25 Last Chance

ListView model from Q_PROPERTY problems

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmllistviewqpropertyqobject
2 Posts 2 Posters 781 Views
  • 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
    Kyeiv
    wrote on 6 Apr 2021, 08:54 last edited by Kyeiv 4 Jun 2021, 08:58
    #1

    I have a Q_OBJECT code:

        Q_PROPERTY(QList<QString> someStrings READ someStrings NOTIFY stringsChanged)
        QList<QString> someStrings();
    
    QList<QString> MyClass::someStrings()
    {
        QList<QString> list;
        for(int i = 0; i < 10; i++)
            list.push_back("something");
        
    
        return list;
    }
    

    and for the given QML code:

                    ListView {
                        id: myListVIew
                        model: ["one", "two"]
                        delegate:
                            Text {
                                id: text
                                text: " element "
                                font.pointSize: 20
                            }
                        Component.onCompleted:
                        {
                            console.log(myListVIew.model)
                        }
                    }
    

    I get the view of:

    element
    element

    and console log:
    qml: [one, two]

    But when i use the model from Q_OBJECT:

                    ListView {
                        id: myListVIew
                        model: my_model.someStrings
                        delegate:
                            Text {
                                id: text
                                text: " element "
                                font.pointSize: 20
                            }
                        Component.onCompleted:
                        {
                            console.log(myListVIew.model)
                        }
                    }
    

    My view is empty, nothing prints to view, but the model is filled as i get this console log:
    qml: [something,something,something,something,something,something,something,something,something,something]

    What am i doing wrong that the view is empty?

    K 1 Reply Last reply 6 Apr 2021, 09:02
    0
    • K Kyeiv
      6 Apr 2021, 08:54

      I have a Q_OBJECT code:

          Q_PROPERTY(QList<QString> someStrings READ someStrings NOTIFY stringsChanged)
          QList<QString> someStrings();
      
      QList<QString> MyClass::someStrings()
      {
          QList<QString> list;
          for(int i = 0; i < 10; i++)
              list.push_back("something");
          
      
          return list;
      }
      

      and for the given QML code:

                      ListView {
                          id: myListVIew
                          model: ["one", "two"]
                          delegate:
                              Text {
                                  id: text
                                  text: " element "
                                  font.pointSize: 20
                              }
                          Component.onCompleted:
                          {
                              console.log(myListVIew.model)
                          }
                      }
      

      I get the view of:

      element
      element

      and console log:
      qml: [one, two]

      But when i use the model from Q_OBJECT:

                      ListView {
                          id: myListVIew
                          model: my_model.someStrings
                          delegate:
                              Text {
                                  id: text
                                  text: " element "
                                  font.pointSize: 20
                              }
                          Component.onCompleted:
                          {
                              console.log(myListVIew.model)
                          }
                      }
      

      My view is empty, nothing prints to view, but the model is filled as i get this console log:
      qml: [something,something,something,something,something,something,something,something,something,something]

      What am i doing wrong that the view is empty?

      K Offline
      K Offline
      KroMignon
      wrote on 6 Apr 2021, 09:02 last edited by
      #2

      @Kyeiv said in ListView model from Q_PROPERTY problems:

      What am i doing wrong that the view is empty?

      The problem is that QList<QString>() is not a valid type for a QML model!

      QML model can be QList<QObject*>() or QStringList() (cf https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html).

      Change your Q_PROPERTY from QList<QString> to QStringList and it will work as expected.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      0

      2/2

      6 Apr 2021, 09:02

      • Login

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