Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to assign the Qlist as model for ListView?
QtWS25 Last Chance

How to assign the Qlist as model for ListView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
listviewmodelqlist
3 Posts 3 Posters 2.1k 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.
  • M Offline
    M Offline
    Mathan M
    wrote on 5 Oct 2016, 14:44 last edited by koahnig 10 May 2016, 14:54
    #1

    Hi,

    I read the values from XML file and store it in the object, and the object stored in the Qlist.This mentioned coded in HomeController.cpp. Now I want to now, how to assign the Qlist to Listview in qml?

    HomeController.h:

    struct PortalMapItemInfo {
    QString pstrTitle;
    QString pstrItemId;
    QString pstrDescription;
    QString pstrCreated;
    };
    
    class HomeController : public QObject
    {
    
    Q_OBJECT
    
    public:
    
    Q_INVOKABLE bool eveReadXML();
    
    }
    

    HomeController.CPP:

    void HomeController::eveReadXML()
    {
    
    //Read the xml Value and load it the object of the class
    PortalMapItemInfo obj;
    
    obj.pstrTitle = strTitle;
    obj.pstrItemId = strItemId;
    obj.pstrDescription = strDescription;
    obj.pstrCreated = strCreated;
    
    QList<PortalMapItemInfo> datalist;
    
    datalist << obj; //Values are properly binded.
    
    }
    

    ListPortalItem.Qml

    ListView {
    id: idListView
    anchors {
    left: parent.left
    leftMargin: 10 * scaleFactor
    right: parent.right
    rightMargin: 10 * scaleFactor
    top: rectangleToolBar.bottom
    topMargin: 10 * scaleFactor
    bottom: rectangleStatusBar.top
    bottomMargin: 10 * scaleFactor
    }
    
    // model:???????????????????????????
    delegate: comsearchDelegate
    spacing: 10 * scaleFactor
    clip: true
    
    
    
    
    
    }
    
    Component {
    id: comsearchDelegate
    
    Row {
    spacing: 10 * scaleFactor
    
    Image {
    id: imageItem
    width: 100 * scaleFactor
    height: 70 * scaleFactor
    source: thumbnailUrl
    }
    
    Column {
    Layout.alignment: Qt.AlignTop
    Text { text: title; font { pixelSize: 14 * scaleFactor; bold: true } }
    
    }
    }
    }
    
    K 1 Reply Last reply 5 Oct 2016, 14:55
    0
    • M Mathan M
      5 Oct 2016, 14:44

      Hi,

      I read the values from XML file and store it in the object, and the object stored in the Qlist.This mentioned coded in HomeController.cpp. Now I want to now, how to assign the Qlist to Listview in qml?

      HomeController.h:

      struct PortalMapItemInfo {
      QString pstrTitle;
      QString pstrItemId;
      QString pstrDescription;
      QString pstrCreated;
      };
      
      class HomeController : public QObject
      {
      
      Q_OBJECT
      
      public:
      
      Q_INVOKABLE bool eveReadXML();
      
      }
      

      HomeController.CPP:

      void HomeController::eveReadXML()
      {
      
      //Read the xml Value and load it the object of the class
      PortalMapItemInfo obj;
      
      obj.pstrTitle = strTitle;
      obj.pstrItemId = strItemId;
      obj.pstrDescription = strDescription;
      obj.pstrCreated = strCreated;
      
      QList<PortalMapItemInfo> datalist;
      
      datalist << obj; //Values are properly binded.
      
      }
      

      ListPortalItem.Qml

      ListView {
      id: idListView
      anchors {
      left: parent.left
      leftMargin: 10 * scaleFactor
      right: parent.right
      rightMargin: 10 * scaleFactor
      top: rectangleToolBar.bottom
      topMargin: 10 * scaleFactor
      bottom: rectangleStatusBar.top
      bottomMargin: 10 * scaleFactor
      }
      
      // model:???????????????????????????
      delegate: comsearchDelegate
      spacing: 10 * scaleFactor
      clip: true
      
      
      
      
      
      }
      
      Component {
      id: comsearchDelegate
      
      Row {
      spacing: 10 * scaleFactor
      
      Image {
      id: imageItem
      width: 100 * scaleFactor
      height: 70 * scaleFactor
      source: thumbnailUrl
      }
      
      Column {
      Layout.alignment: Qt.AlignTop
      Text { text: title; font { pixelSize: 14 * scaleFactor; bold: true } }
      
      }
      }
      }
      
      K Offline
      K Offline
      koahnig
      wrote on 5 Oct 2016, 14:55 last edited by
      #2

      @Mathan-M

      I have added code wrappers to your post. Please use them. You can introduce them by using the right most button above the edit window.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on 5 Oct 2016, 15:06 last edited by
        #3

        The easy way:
        get rid of PortalMapItemInfo, store the values in 4 different QStringList and use a QStringListModel

        The proper way:
        in HomeController.h add Q_DECLARE_METATYPE(PortalMapItemInfo) http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE

        Instead of a List store your data in a 1-column QStandardItemModel and expose it via a Q_PRPERTY

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2

        1/3

        5 Oct 2016, 14:44

        • Login

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