From C++ (Qt):

Qt Code:
  1. int main(int argc, char *argv[])
  2. {
  3. QApplication app(argc, argv);
  4.  
  5. /*
  6.   * I have omitted the code about connection with the database and all for ease of
  7.   * viewing the code.
  8.   */
  9.  
  10.  
  11. // My own function for filling in the data in the `QSqlQueryModel`
  12. QString binid = "B1";
  13. QString query = "SELECT t1.BinId, t1.PartitionId, t2.UnitId, t2.ItemCount FROM Bin_Partitions AS t1 "
  14. "INNER JOIN Partition_Units AS t2 ON t1.PartitionId = t2.PartitionId "
  15. "WHERE t1.BinId ='" + binid + "'";
  16. model->setQuery(query);
  17.  
  18. /*
  19.   * I can see that the query runs successfully because the following
  20.   * QTableView DOES get populated properly.
  21.   */
  22. // Use QTableView to visualize
  23. QTableView *view = new QTableView;
  24. view->setModel(model);
  25. view->show();
  26.  
  27. QQmlApplicationEngine engine;
  28. // Passing the same model to QML for displaying in the TableView.
  29. engine.rootContext()->setContextProperty ("SQQL", model);
  30. engine.load(QUrl(QStringLiteral("/home/.../main.qml")));
  31.  
  32. QObject *topLevel = engine.rootObjects ().value (0);
  33. QQuickWindow *window = qobject_cast <QQuickWindow *> (topLevel);
  34.  
  35. return app.exec();
  36. }
To copy to clipboard, switch view to plain text mode 

From QML:
Qt Code:
  1. import QtQuick 2.2
  2. import QtQuick.Window 2.1
  3. import QtQuick.Controls 1.2
  4.  
  5. Window
  6. {
  7. visible: true
  8. width: 360
  9. height: 360
  10. color: "blue"
  11.  
  12. TableView
  13. {
  14. TableViewColumn{ role: "col1" ; title: "BinId" ; visible: true}
  15. TableViewColumn{ role: "col2" ; title: "PartitionId" }
  16. TableViewColumn{ role: "col3" ; title: "UnitId" }
  17. TableViewColumn{ role: "col4" ; title: "ItemCount" }
  18.  
  19. model: SQQL
  20. }
  21. }
To copy to clipboard, switch view to plain text mode 

The TableView of QML shows up EMPTY!

I need help.