Results 1 to 4 of 4

Thread: Easy way to use QList<int> as data model for ListView?

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Easy way to use QList<int> as data model for ListView?

    Is there any way I can easily use QList<int> as a model for QML Listview?

    In my C++ I have this property:
    Qt Code:
    1. Q_PROPERTY(QList<int> years READ years CONSTANT)
    To copy to clipboard, switch view to plain text mode 

    And was hoping I could just throw that into a ListView. However I get this error when the component is created:

    ASSERT: "list.type() != QQmlListAccessor::Instance" in file util\qqmladaptormodel.cpp, line 925

    I could create a custom model by extending QAbstractItemModel, but before I do that, is there an easier way to do it? Seems kind overboard for just a list of ints. QQmlListProperty is only for QObject types.

    Thanks

  2. #2
    Join Date
    Sep 2008
    Location
    Munich
    Posts
    32
    Thanked 8 Times in 6 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Easy way to use QList<int> as data model for ListView?

    You could use a QVariantList. But then you would need first to convert your QList<int> into a QVariantList (using foreach for example).

  3. The following user says thank you to jryannel for this useful post:

    pherthyl (16th September 2013)

  4. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Easy way to use QList<int> as data model for ListView?

    Thanks.. Another thing that was non-obvious to me was how to then access the data in the delegate. You have to use the modelData property.

  5. #4
    Join Date
    Sep 2013
    Posts
    10
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Easy way to use QList<int> as data model for ListView?

    You should better override a QStandardModel, than the abstract one, and "this->setItemRoleNames(QHash...)" in the constructor.

Similar Threads

  1. ListView problem with c++ model
    By nick85 in forum Qt Quick
    Replies: 7
    Last Post: 12th September 2013, 09:33
  2. Replies: 3
    Last Post: 15th June 2013, 12:39
  3. ListView model binding to QAbstractListModel
    By michalk in forum Qt Quick
    Replies: 1
    Last Post: 16th July 2011, 09:21
  4. Replies: 2
    Last Post: 21st December 2010, 20:43
  5. Easy algorithm to encrypt / decrypt string data?
    By Mike in forum Qt Programming
    Replies: 7
    Last Post: 2nd March 2006, 06:42

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.