Results 1 to 9 of 9

Thread: Cannot display qsqlquerymodel to a qml list view

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.