PDA

View Full Version : Setting QSqlTableModel from c++ to .qml tableView



heczaco
19th January 2016, 20:18
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

posted a

anda_skoa
19th January 2016, 21:32
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,
_

heczaco
20th January 2016, 14:42
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?

anda_skoa
20th January 2016, 17:20
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)
}

Where are the TableViewColumn elements?



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,
_

heczaco
20th January 2016, 22:32
adding the columns makes no difference :/ and QSqlTableModel is a subclass of QAbstractTableModel... is that wrong?

anda_skoa
21st January 2016, 05:50
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.



and QSqlTableModel is a subclass of QAbstractTableModel... is that wrong?
So object is a plain QSqlTableModel not some application specific subclass?

Cheers,
_

heczaco
21st January 2016, 13:33
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 :/


TableView{
id: table;
width: parent.width -100
height: parent.height -200
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 90
model: eventModel.getTData(GData.tName, table);
TableViewColumn{
role:"time"
title:"time"
}
TableViewColumn{
role:"var1"
title:"var1"
}
TableViewColumn{

}
TableViewColumn{
role:"var3"
}
}

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++

11656

anda_skoa
21st January 2016, 18:16
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,
_

heczaco
21st January 2016, 21:14
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?

anda_skoa
22nd January 2016, 10:58
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,
_

heczaco
25th January 2016, 21:41
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?

anda_skoa
26th January 2016, 08:17
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.



Can I only use that role?

Of course not, that would make models pretty useless.



If so, how do I get the other columns to show?
See comment #10.

Cheers,
_