Results 1 to 12 of 12

Thread: Setting QSqlTableModel from c++ to .qml tableView

  1. #1
    Join Date
    Jan 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Setting QSqlTableModel from c++ to .qml tableView

    I have a tableview on a qmlfile, I'm trying to set the model from c++.

    The model I'm using is QSqlTableModel, if a create a new tableview and set the model from c++ it seems to work, but when I pass the model to the model property in the qmlfile it doesn't work and I can't figure out why...

    the c++ model code is:
    Qt Code:
    1. QSqlTableModel * SqlEventModel::getTableData(QString tableName, QTableView *item){
    2.  
    3. const QString queryStr = "SELECT sql FROM sqlite_master WHERE tbl_name = '" + tableName +"' AND type = 'table'" ;
    4. QSqlQuery query(queryStr);
    5. if (!query.exec())
    6. qFatal("Query failed");
    7.  
    8.  
    9. QSqlDatabase db = QSqlDatabase::database(":memory:");
    10.  
    11. QSqlTableModel *model = new QSqlTableModel(item, db);
    12. model->setTable(tableName);
    13. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    14. model->select();
    15.  
    16.  
    17. int i=0;
    18. while (query.next()){
    19. model->setHeaderData(i, Qt::Vertical, query.value(i).toString());
    20. i++;
    21. }
    22.  
    23. return model;
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 
    that returns a model which works if within c++ I create a tableview and assign the model but when I do this:

    Qt Code:
    1. TableView{
    2. id: table;
    3. width: parent.width -100
    4. height: parent.height -200
    5. anchors.horizontalCenter: parent.horizontalCenter
    6. anchors.top: parent.top
    7. anchors.topMargin: 90
    8. model: SqlEventModel.getTableData(GData.tName,table)
    9. }
    To copy to clipboard, switch view to plain text mode 
    it doesn't work, it just shows an empty table even though when I debug the tableview model is not empty...

    if I add:

    Qt Code:
    1. QTableView *view = new QTableView();
    2. view->setModel(model);
    3. view->show();
    To copy to clipboard, switch view to plain text mode 

    just before the return on the c++ function I get a new window with a table which properly displays the table... I tried adding columns with the same rolename that I give in c++ and they won't populate either.... here's a screenshot of both tables

    http://i.stack.imgur.com/gGzhu.png

    posted a

  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: Setting QSqlTableModel from c++ to .qml tableView

    How is your table view configured?
    Do you have the "display" property as one of the columns?

    You should be able to see the first colum of the table module using "display".

    Of course all other column will first have to be mapped to properties.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Setting QSqlTableModel from c++ to .qml tableView

    I have my tableview configured just as the code above

    TableView{
    id: table;
    width: parent.width -100
    height: parent.height -200
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.top: parent.top
    anchors.topMargin: 90
    model: SqlEventModel.getTableData(GData.tName,table)
    }

    SqlEventModel.getTableData(GData.tName,table) calls the c++ function on the code above which returns the model, if I set that model to a tableview I create from c++ the table is displayed just fine, but when I set that tables model it doesn't display anything...

    I can't seem to find any property called display... does that property belong to TableView?

  4. #4
    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: Setting QSqlTableModel from c++ to .qml tableView

    Quote Originally Posted by heczaco View Post
    I have my tableview configured just as the code above
    Qt Code:
    1. TableView{
    2. id: table;
    3. width: parent.width -100
    4. height: parent.height -200
    5. anchors.horizontalCenter: parent.horizontalCenter
    6. anchors.top: parent.top
    7. anchors.topMargin: 90
    8. model: SqlEventModel.getTableData(GData.tName,table)
    9. }
    To copy to clipboard, switch view to plain text mode 
    Where are the TableViewColumn elements?

    Quote Originally Posted by heczaco View Post
    I can't seem to find any property called display... does that property belong to TableView?
    That is one of the standard properties of a model. I assume your QSqlTableModel is actually a subclass that maps additional properties onto columns?

    Cheers,
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Setting QSqlTableModel from c++ to .qml tableView

    adding the columns makes no difference :/ and QSqlTableModel is a subclass of QAbstractTableModel... is that wrong?

  6. #6
    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: Setting QSqlTableModel from c++ to .qml tableView

    Quote Originally Posted by heczaco View Post
    adding the columns makes no difference :/
    Without columns it wil definitely be empty, because then you don't thell the view which property to show in which column.

    And you forgot to post the code with coumns.

    Quote Originally Posted by mayrhofer
    and QSqlTableModel is a subclass of QAbstractTableModel... is that wrong?
    So object is a plain QSqlTableModel not some application specific subclass?

    Cheers,
    _

  7. #7
    Join Date
    Jan 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Setting QSqlTableModel from c++ to .qml tableView

    Well, for the columns I tried many ways, with rolenames with rolenames equal as the ones i'd get if I create the table from c++ (as in the code in the original post), with titles, no titles, etc. None seems to work :/

    Qt Code:
    1. TableView{
    2. id: table;
    3. width: parent.width -100
    4. height: parent.height -200
    5. anchors.horizontalCenter: parent.horizontalCenter
    6. anchors.top: parent.top
    7. anchors.topMargin: 90
    8. model: eventModel.getTData(GData.tName, table);
    9. TableViewColumn{
    10. role:"time"
    11. title:"time"
    12. }
    13. TableViewColumn{
    14. role:"var1"
    15. title:"var1"
    16. }
    17. TableViewColumn{
    18.  
    19. }
    20. TableViewColumn{
    21. role:"var3"
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    The table model is a plain QSqlTableModel, does the problem lie in there?, 'cause it seems to work just fine for a table created in c++

    Capture.jpg

  8. #8
    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: Setting QSqlTableModel from c++ to .qml tableView

    The only roles you have are the ones defined in QAbstractItemModel, e.g. "display".
    None of your columns use existing role names, so it is no suprise that the table is empty.

    Cheers,
    _

  9. #9
    Join Date
    Jan 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Setting QSqlTableModel from c++ to .qml tableView

    thanks, in debug time I saw that the role names were totally different to the headers (I thought set headerdata would change both) it shows data now that I change the roles to the names I saw on debug time, but I'm not able to set the roles from c++ to what I want, could you help me get in the right direction?

  10. #10
    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: Setting QSqlTableModel from c++ to .qml tableView

    QAbstractItemModel::roleNames() allows you to map custom roles name to custom role values.

    So you specifiy some integer role values for the data you want to get (usually usign an enum) and then map these values to the strings that you want to use on the QML side.

    Cheers,
    _

  11. #11
    Join Date
    Jan 2016
    Posts
    6
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Setting QSqlTableModel from c++ to .qml tableView

    Hi, I'm only able to display the first column with the Display Role, Can I only use that role? If so, how do I get the other columns to show?

  12. #12
    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: Setting QSqlTableModel from c++ to .qml tableView

    Quote Originally Posted by heczaco View Post
    Hi, I'm only able to display the first column with the Display Role
    Unsurprisingly, since that is one of the standard roles that is also in the standard role name mapping.

    Quote Originally Posted by heczaco View Post
    Can I only use that role?
    Of course not, that would make models pretty useless.

    Quote Originally Posted by heczaco View Post
    If so, how do I get the other columns to show?
    See comment #10.

    Cheers,
    _

Similar Threads

  1. Default sort in QSqlTableModel and tableview
    By rdjenner in forum Qt Programming
    Replies: 0
    Last Post: 17th August 2011, 17:15
  2. Replies: 7
    Last Post: 30th June 2011, 10:16
  3. Replies: 0
    Last Post: 17th June 2011, 09:11
  4. QSqlTableModel Setting Current Row?
    By fifth in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2008, 13:44
  5. setting UserRole data in QSqlTableModel
    By orgads in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2008, 09:40

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.