Results 1 to 9 of 9

Thread: Cannot display qsqlquerymodel to a qml list view

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: Cannot display qsqlquerymodel to a qml list view

    You can just have the hash on the stack, no need for allocating it on the heap using new.
    Qt Code:
    1. QHash<int,QByteArray> hash;
    To copy to clipboard, switch view to plain text mode 

    Currently you leak it.

    Cheers,
    _

  2. #2
    Join Date
    Sep 2014
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Cannot display qsqlquerymodel to a qml list view

    you mean like following?

    Qt Code:
    1. class SqlQueryModel: public QSqlQueryModel
    2. {
    3. Q_OBJECT
    4. QHash<int,QByteArray> hash;
    5. public:
    6. explicit SqlQueryModel() : QSqlQueryModel()
    7. {
    8. //hash = new QHash<int,QByteArray>;
    9. hash.insert(Qt::UserRole, QByteArray("name"));
    10. hash.insert(Qt::UserRole + 1, QByteArray("title"));
    11. }
    12. QVariant data(const QModelIndex &index, int role) const
    13. {
    14. if(role < Qt::UserRole) {
    15. return QSqlQueryModel::data(index, role);
    16. }
    17. QSqlRecord r = record(index.row());
    18. return r.value(QString(hash.value(role))).toString();
    19. }
    20. inline QHash<int, QByteArray> roleNames() const { return hash; }
    21. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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: Cannot display qsqlquerymodel to a qml list view

    Exactly!

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 25th July 2013, 21:56
  2. Replies: 1
    Last Post: 14th November 2012, 21:00
  3. List View with sections for alphabetialy sorted list
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 12th October 2010, 11:50
  4. Replies: 8
    Last Post: 6th May 2010, 11:17
  5. how to display QSqlQueryModel in QTreeView
    By mandal in forum Newbie
    Replies: 6
    Last Post: 2nd September 2009, 08:25

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.