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. Using QStringListModel as model in ListView
Forum Updated to NodeBB v4.3 + New Features

Using QStringListModel as model in ListView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 2.2k 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.
  • S Offline
    S Offline
    SmokeyTheBandit
    wrote on 27 Jun 2018, 19:30 last edited by SmokeyTheBandit
    #1

    Hello everybody. I'm currently playing around with Qt Quick. I'm trying to write an remote control application for qBittorrent. Through its web API it is possible to fetch all kinds of information. The software allows you to sort torrents into different categories. I wanted to list these catagories using a ListView.

    I've written the non-ui code in C++. In C++ the data is fetched and parsed, and made available to QML. I've put the source code on github.

    This function returns all the categories
    This line of qml code is giving me issues

    I can access this property from QML without any issues. If I set the delegate of the ListView to:

    delegate: Text {
        text: display
        width: parent.width
    }
    

    The code works as expected, I get the following result
    Listview using Text delegate

    However if i change the Delegate from Text to ItemDelegate, the text is set to '2'

    delegate: ItemDelegate {
       text: display
       width: parent.width
    }
    

    Using this code, I get the following result
    Listview using ItemDelegate delegate

    I'm absolutely clueless of why this is happening. Any help is greatly appreciated.

    1 Reply Last reply
    0
    • 6 Offline
      6 Offline
      6thC
      wrote on 28 Jun 2018, 00:32 last edited by
      #2

      http://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#qml-data-models

      I assume display is the property name you want? This rough as guts ListModel driven copy and slight modification works for me - perhaps your model?

      QStringListModel *categories(); // <= can you make this a public slot and call it directly?

      I use QStringList results from c++ to drive QML models everywhere, but I don't invoke function pointers like that. I'll use a public slot or a Q_INVOKABLE (but I never do I just use a public slot)
      c++

      public slots:
          QStringList getSomethingPlurals();
      

      qml:

      var cppCopyModel    = NamedInstanceQMLContextCpp.getSomethingPlurals();
      // OR
       property ListModel seriesModel : NamedInstanceQMLContextCpp.getSomethingPlurals();
      

      You code (kinda) with a QML ListModel seemingly working fine...

      
      ApplicationWindow {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
          id: root // window
          color: "black"
      
          ListModel {id : modelInstance
              ListElement {propertyNameUnused: "first"; name:"aaa";}
              ListElement {propertyNameUnused: "second"; name:"bbb";}
              ListElement {propertyNameUnused: "third"; name:"ccc";}
          }
      
          Item {
              anchors.fill: parent
              ListView {
                         id: listView
                         anchors.fill: parent
                         headerPositioning: ListView.OverlayHeader
                         header: Pane {
                             id: header
                             z: 2
                             width: parent.width
                             contentHeight: lbl_status.height
      
                             Text {
                                 id: lbl_status
                                 text: "Status"
                             }
      
                             MenuSeparator {
                                 parent: header
                                 width: parent.width
                                 anchors.verticalCenter: parent.bottom
                             }
                         }
      
                         footer: ItemDelegate {
                             id: footer
                             text: qsTr("Footer")
                             width: parent.width
      
                             MenuSeparator {
                                 parent: footer
                                 width: parent.width
                                 anchors.verticalCenter: parent.top
                             }
                         }
      
                         model: modelInstance // Torrents.categories
      
                         delegate:  ItemDelegate { // Text {
                           //  color: "white" // works with Text type
                             text: name
                             width: parent.width
                         }
      
                         ScrollIndicator.vertical: ScrollIndicator { }
                     }
          }
      

      HTH

      1 Reply Last reply
      1
      • G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 28 Jun 2018, 12:25 last edited by
        #3

        ItemDelegate has a display property : https://doc.qt.io/qt-5/qml-qtquick-controls2-abstractbutton.html#display-prop .
        To dipslay the display role, you have to explicitely specifiy it like so:
        text: model.display, this is usually preferable since it prevents shadowing and make it clearer where the value comes from.

        S 1 Reply Last reply 2 Jul 2018, 09:10
        3
        • G GrecKo
          28 Jun 2018, 12:25

          ItemDelegate has a display property : https://doc.qt.io/qt-5/qml-qtquick-controls2-abstractbutton.html#display-prop .
          To dipslay the display role, you have to explicitely specifiy it like so:
          text: model.display, this is usually preferable since it prevents shadowing and make it clearer where the value comes from.

          S Offline
          S Offline
          SmokeyTheBandit
          wrote on 2 Jul 2018, 09:10 last edited by
          #4

          @GrecKo If I use text: model.display, the text is displayed correctly. Thanks for your help!

          1 Reply Last reply
          1

          1/4

          27 Jun 2018, 19:30

          • Login

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