Results 1 to 2 of 2

Thread: How to assign Qlist as a model to ListView?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2016
    Posts
    53
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default How to assign Qlist as a model to ListView?

    Hi,

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



    HomeController.h:
    -----------------------
    Qt Code:
    1. struct PortalMapItemInfo {
    2. QString pstrTitle;
    3. QString pstrItemId;
    4. QString pstrDescription;
    5. QString pstrCreated;
    6. };
    7. class HomeController : public QObject
    8. {
    9.  
    10. Q_OBJECT
    11.  
    12. public:
    13.  
    14. Q_INVOKABLE bool eveReadXML();
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    HomeController.CPP:
    -----------------------
    Qt Code:
    1. void HomeController::eveReadXML()
    2. {
    3.  
    4. //Read the xml Value and load it the object of the class
    5. PortalMapItemInfo obj;
    6.  
    7. obj.pstrTitle = strTitle;
    8. obj.pstrItemId = strItemId;
    9. obj.pstrDescription = strDescription;
    10. obj.pstrCreated = strCreated;
    11.  
    12. QList<PortalMapItemInfo> datalist;
    13.  
    14. datalist << obj; //Values are properly binded.
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    ListPortalItem.Qml
    --------------------
    Qt Code:
    1. ListView {
    2. id: idListView
    3. anchors {
    4. left: parent.left
    5. leftMargin: 10 * scaleFactor
    6. right: parent.right
    7. rightMargin: 10 * scaleFactor
    8. top: rectangleToolBar.bottom
    9. topMargin: 10 * scaleFactor
    10. bottom: rectangleStatusBar.top
    11. bottomMargin: 10 * scaleFactor
    12. }
    13.  
    14. // model:???????????????????????????
    15. delegate: comsearchDelegate
    16. spacing: 10 * scaleFactor
    17. clip: true
    18.  
    19.  
    20.  
    21.  
    22.  
    23. }
    24.  
    25. Component {
    26. id: comsearchDelegate
    27.  
    28. Row {
    29. spacing: 10 * scaleFactor
    30.  
    31. Image {
    32. id: imageItem
    33. width: 100 * scaleFactor
    34. height: 70 * scaleFactor
    35. source: thumbnailUrl
    36. }
    37.  
    38. Column {
    39. Layout.alignment: Qt.AlignTop
    40. Text { text: title; font { pixelSize: 14 * scaleFactor; bold: true } }
    41.  
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 6th October 2016 at 17:29. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to assign Qlist as a model to ListView?

    Since your list entry has multiple value fields, I would suggest a QAbstractListModel as the base class instead of a simple QObject.

    You have to implement roleNames() to generate a mapping from QML side role names to C++ role integer values, data to return the respective value and rowCount() as the size of the list.

    Cheers,
    _

Similar Threads

  1. How to assign SQL query output model from Qt to QML's TableView?
    By TheIndependentAquarius in forum Qt Quick
    Replies: 3
    Last Post: 9th January 2015, 08:04
  2. How to assign model roles to delegates
    By ustulation in forum Qt Quick
    Replies: 8
    Last Post: 13th February 2014, 16:10
  3. Replies: 3
    Last Post: 17th September 2013, 19:40
  4. ListView problem with c++ model
    By nick85 in forum Qt Quick
    Replies: 7
    Last Post: 12th September 2013, 10:33
  5. ListView model binding to QAbstractListModel
    By michalk in forum Qt Quick
    Replies: 1
    Last Post: 16th July 2011, 10:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.