Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting QSqlTableModel from c++ to .qml tableView
QtWS25 Last Chance

Setting QSqlTableModel from c++ to .qml tableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
tableview tablemodelc++
2 Posts 2 Posters 2.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    heczaco
    wrote on last edited by heczaco
    #1

    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:
    QSqlTableModel * SqlEventModel::getTableData(QString tableName, QTableView *item){

    	const QString queryStr = "SELECT sql FROM sqlite_master WHERE tbl_name = '" + tableName +"' AND type = 'table'" ;
    	QSqlQuery query(queryStr);
    	if (!query.exec())
    		qFatal("Query failed");
    
    
    	QSqlDatabase db = QSqlDatabase::database(":memory:");
    
    	QSqlTableModel *model = new QSqlTableModel(item, db);
    	model->setTable(tableName);
    	model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    	model->select();
    
    
    	int i=0;
    	while (query.next()){
    		model->setHeaderData(i, Qt::Vertical, query.value(i).toString());
    		i++;
    	}
    
       return   model;
    
    }
    

    that returns a model which works if within c++ I create a tableview and assign the model but when I do this:

    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)
    }
    

    it doesn't work, it just shows an empty table even though when I debug the tableview model is not empty...

    if I add:

    QTableView *view =  new QTableView();
    view->setModel(model);
    view->show();
    

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      To make models accessible to QML you have to do sum customization like described in the QtQuick Model Views Data documentation.

      QSqlTableModel example here

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved