Results 1 to 8 of 8

Thread: QVariantList/QVariantMap to QML ListModel

  1. #1
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QVariantList/QVariantMap to QML ListModel

    Hi,

    I'm using an QAbstractItemModel to display data on a QML TreeView. I have a list of items for a particular column in the table.

    I have saved those items in a QVariantList. Is it possible to display this on the UI as a ListView?

    Qt Code:
    1. QVariant DiffModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. DiffInfo* item = getItem(index);
    7.  
    8. switch (role)
    9. {
    10. case ColumnRole:
    11. return item->rightColumn();//Return a QVariantList
    12. }
    13. return QVariant();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Kindly advice

  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: QVariantList/QVariantMap to QML ListModel

    A variant list is accessible as a JavaScript array on the QML side.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVariantList/QVariantMap to QML ListModel

    @anda_skoa thanks for ur reply

    Could you provide me an example to access the qvariantlist in qml?

    Is it possible to use qvariantlist in a qml list view?

  4. #4
    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: QVariantList/QVariantMap to QML ListModel

    Quote Originally Posted by volcano View Post
    Could you provide me an example to access the qvariantlist in qml?
    http://www.w3schools.com/js/js_arrays.asp

    Quote Originally Posted by volcano View Post
    Is it possible to use qvariantlist in a qml list view?
    Yes, just use it as the view's model.
    Each delegate can access its respective entry as "model.modelData"

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    volcano (17th December 2016)

  6. #5
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVariantList/QVariantMap to QML ListModel

    Thanks anda_skoa

    If I use a qvariantmap, what role do I use for key and value?

    Next problem I am facing is that, the items is the list view vary. As this list is displayed within a row of the table view, I require the each row to have variable height. Is it possible?

    Kindly advice
    Last edited by volcano; 17th December 2016 at 08:13.

  7. #6
    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: QVariantList/QVariantMap to QML ListModel

    Quote Originally Posted by volcano View Post
    If I use a qvariantmap, what role do I use for key and value?
    They key of the map is the role name in QML. I.e. you access the map like a JavaScript object

    Quote Originally Posted by volcano View Post
    Next problem I am facing is that, the items is the list view vary. As this list is displayed within a row of the table view, I require the each row to have variable height. Is it possible?
    Hmm, I haven't used TreeView myself yet, but I don't see why the item delegate couldn't have different height for different content.

    Cheers,
    _

  8. #7
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVariantList/QVariantMap to QML ListModel

    Thanks anda_skoa

    My qvariantmap has different keys for each item. I found a link http://doc.qt.io/qt-5/qtqml-cppinteg...ray-and-object which mentions how to use the qvariantmap using a for loop

    However I am using this in a list view. So I am not sure what to specify for the key role and value role.

    Regarding the variable height for each row, let's consider a table view. I am displaying 3 columns(one label, second a list view and third a list view) using tableviewcolumn. And each has its own delegate. So do i set the height in the itemdelegate ? Or is there a better way?
    Last edited by volcano; 17th December 2016 at 11:43.

  9. #8
    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: QVariantList/QVariantMap to QML ListModel

    Quote Originally Posted by volcano View Post
    TMy qvariantmap has different keys for each item.
    Since you retrieve this from C++ it would be easier if you returned a list.

    A map is only useful if you have more than one data aspect per item.

    If you insist on using a less ideal data type, then you need to get the keys as a list to feed to the view's model property.
    Inside the delegate you then use the current key as proivded by that list to do the data lookup.

    Quote Originally Posted by sylas View Post
    Regarding the variable height for each row, let's consider a table view.
    Originally you wrote TreeView, so you are using a TableView now?

    Quote Originally Posted by sylas View Post
    I am displaying 3 columns(one label, second a list view and third a list view) using tableviewcolumn. And each has its own delegate. So do i set the height in the itemdelegate ? Or is there a better way?
    Well, my first try would be to make the delegate as large as you want it to be.

    Cheers,
    _

Similar Threads

  1. How to access key and value in QvariantMap?
    By Mathan in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2016, 19:21
  2. Convery QByteArray to QVariantMap
    By karankumar1609 in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2013, 21:19
  3. Traversing QVariantMap
    By ggdev001 in forum Newbie
    Replies: 9
    Last Post: 7th February 2013, 17:07
  4. Replies: 0
    Last Post: 16th May 2010, 17:53
  5. QVariant, QVariantMap, and QFileInfoList
    By themolecule in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 07:00

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.