Results 1 to 4 of 4

Thread: How to assign SQL query output model from Qt to QML's TableView?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to assign SQL query output model from Qt to QML's TableView?

    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.

  2. #2
    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: How to assign SQL query output model from Qt to QML's TableView?

    Where in your code do you map the columns from the SqlQueryModel into these roles you are using in QML?
    Does the roleNames() method of the model return these?

    Cheers,
    _

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

    TheIndependentAquarius (9th January 2015)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to assign SQL query output model from Qt to QML's TableView?

    Quote Originally Posted by anda_skoa View Post
    Where in your code do you map the columns from the SqlQueryModel into these roles you are using in QML?
    Does the roleNames() method of the model return these?
    Thankful for your time. I am sorry, I didn't know that something like that needs to be done.
    Could you please point me out to a documentation which shows what and how needs to be done for
    mapping the columns from the SqlQueryMode?

  5. #4
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to assign SQL query output model from Qt to QML's TableView?

    I found the hints here:
    http://qt-project.org/wiki/How_to_us...ryModel_in_QML

    It is done now.
    Thanks.

Similar Threads

  1. How to assign model roles to delegates
    By ustulation in forum Qt Quick
    Replies: 8
    Last Post: 13th February 2014, 15:10
  2. Tableview after Model move section
    By poporacer in forum Newbie
    Replies: 5
    Last Post: 14th September 2011, 01:13
  3. tableview , model and owncolumn
    By maston in forum Qt Programming
    Replies: 6
    Last Post: 28th August 2010, 11:00
  4. tableview model view problem
    By skuda in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 14:02
  5. I can't assign TableView signals...
    By tomek in forum Newbie
    Replies: 5
    Last Post: 9th January 2006, 21:04

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.