Results 1 to 9 of 9

Thread: Cannot display qsqlquerymodel to a qml list view

  1. #1
    Join Date
    Feb 2012
    Posts
    9
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Cannot display qsqlquerymodel to a qml list view

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QGuiApplication app(argc, argv);
    4.  
    5.  
    6. QFile f("productList.sqlite");
    7.  
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setHostName("localhost");
    10. db.setDatabaseName("productdb");
    11. db.setUserName("user");
    12. db.open();
    13. QTextStream in(&f);
    14. QString line;
    15. if (f.open(QFile::ReadOnly | QFile::Text)) {
    16. QTextStream in(&f);
    17. line = in.readLine();
    18. while(!line.isNull()) {
    19. QSqlQuery query = db.exec(line);
    20. qDebug() << line;
    21. line = in.readLine();
    22. }
    23. }
    24.  
    25. f.close();
    26.  
    27. QSqlQuery query1 = db.exec("SELECT * FROM productList");
    28. qDebug() << query1.size();
    29.  
    30. model->setQuery("SELECT * FROM productList", db);
    31. qDebug() << model->record(0);
    32. model->setHeaderData(0, Qt::Horizontal, QObject::tr("Categorylv1"));
    33. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Categorylv2"));
    34.  
    35. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Categorylv3"));
    36. model->setHeaderData(3, Qt::Horizontal, QObject::tr("productCode"));
    37. model->setHeaderData(4, Qt::Horizontal, QObject::tr("productDescription"));
    38. model->setHeaderData(5, Qt::Horizontal, QObject::tr("productPrice"));
    39.  
    40. QtQuick2ApplicationViewer viewer;
    41. viewer.rootContext()->setContextProperty("myModel", model);
    42. viewer.setMainQmlFile(QStringLiteral("qml/basket/main.qml"));
    43. viewer.showExpanded();
    44.  
    45. return app.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle {
    4. width: 360
    5. height: 360
    6. // color: "black"
    7.  
    8. Grid {
    9. id: grid1
    10. anchors.rightMargin: 0
    11. anchors.bottomMargin: 0
    12. anchors.leftMargin: 0
    13. anchors.topMargin: 0
    14. anchors.fill: parent
    15.  
    16. Text {
    17. id: text1
    18. text: qsTr("Text")
    19. anchors.right: parent.right
    20. anchors.left: parent.left
    21. anchors.top: parent.top
    22. font.pixelSize: 12
    23. }
    24.  
    25. ListView {
    26. id: listView1
    27. model:myModel
    28. x: 0
    29. y: 0
    30. anchors.topMargin: 27
    31. anchors.fill: parent
    32. delegate: Item {
    33.  
    34. Row {
    35. anchors.margins: 4
    36. anchors.fill: parent
    37. spacing: 4;
    38.  
    39. Text {
    40. text: productDescription
    41. width: 150
    42. }
    43. }
    44. }
    45. }
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    Hello everyone I try some code with qml and i want to see to a listview some items with productsDescription I tried the example i found to web but the result is an empty list any ideas what I do wrong?
    Last edited by ironexmaiden; 3rd August 2014 at 12:53.

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

    Default Re: Cannot display qsqlquerymodel to a qml list view

    Do you get the delegate instantiated but without data or does not delegate get instantiated at all?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2012
    Posts
    9
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cannot display qsqlquerymodel to a qml list view

    Consider problem SOLVED i solved it with implementing

    Qt Code:
    1. class SqlQueryModel: public QSqlQueryModel
    2. {
    3. Q_OBJECT
    4. QHash<int,QByteArray> *hash;
    5. public:
    6. explicit SqlQueryModel(QObject * parent) : QSqlQueryModel(parent)
    7. {
    8. hash = new QHash<int,QByteArray>;
    9. hash->insert(Qt::UserRole, QByteArray("someRoleName"));
    10. hash->insert(Qt::UserRole + 1, QByteArray("otherRoleName"));
    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 RoleNameHash roleNames() const { return *hash; }
    21. };
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Cannot display qsqlquerymodel to a qml list view

    Hey there,

    I am trying the same since some days now. Can you maybe post the whole project as a zip file?
    Or post the main.cpp and how you implement it?

    Would be awsome...

    inline RoleNameHash roleNames() const { return *hash; }

    does not name a type, is one of the errors i get...
    Last edited by grol; 1st September 2014 at 20:43.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Cannot display qsqlquerymodel to a qml list view

    Just implement this method http://qt-project.org/doc/qt-5/qabst...html#roleNames

    ironexmaiden probably had a typedef for the return type.

    Cheers,
    _

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

    Default Re: Cannot display qsqlquerymodel to a qml list view

    solved like following. thanks!

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

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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,
    _

  8. #8
    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 

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 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.