[QML] Access the value of a different column in TableView
-
I have the following code:
TableView { id: table width:parent.width *0.95 anchors.horizontalCenter: parent.horizontalCenter anchors.top:_descriptionPoste.bottom anchors.topMargin: 10 selectionMode: SelectionMode.SingleSelection itemDelegate :editableDelegate TableViewColumn{ role: "min" ; title: "Nb. min" ;width:(tableauTours.width/5); delegate: minComponent} TableViewColumn{ role: "max" ; title: "Nb. max" ;width:(tableauTours.width/5); delegate: maxComponent} }
Inside the component i found that i could have access (in the component) to the current data ( styleData.value ) , the current row index ( styleData.row ) and the current column index ( styleData.column) .
However, if i'm clicking on the minComponent for example, i can have access the the min value, but how can i have access to the max value of the current row ?Thanks for reading
-
When i'm trying :
table.model.get(0).idI get the following output :
TypeError: Property 'get' of object SqlQueryModel(0x2599230) is not a function
This is because the model come from a SqlDatabase i got from a class i created called SqlQueryModel that inherit from QSqlQueryModel.
What can I do ?
-
When i'm trying :
table.model.get(0).idI get the following output :
TypeError: Property 'get' of object SqlQueryModel(0x2599230) is not a function
This is because the model come from a SqlDatabase i got from a class i created called SqlQueryModel that inherit from QSqlQueryModel.
What can I do ?
-
Ok, here is the code i implemented :
In SqlQueryModel.h
Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;In SqlQueryModel.cpp
QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole)
{
value = QSqlQueryModel::data(index, role);
}
else
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
qDebug() << "Error: " << lastError();
return value;
}And here is my qml code :
table.model.data(0,0)but it keep returning "undefined", do you know why ?
-
Ok, here is the code i implemented :
In SqlQueryModel.h
Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;In SqlQueryModel.cpp
QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole)
{
value = QSqlQueryModel::data(index, role);
}
else
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
qDebug() << "Error: " << lastError();
return value;
}And here is my qml code :
table.model.data(0,0)but it keep returning "undefined", do you know why ?
@Kaxu
data
is already a method inQSqlQueryModel
. Moreover QML won't understand QModelIndex too.
Instead create a separateQ_INVOKABLE
method. Since you are using QSqlQueryModel you can use record to get row at particular index and then extract the data
For eg://in header file Q_INVOKABLE QString getData(int row); //in source QString getData(int row) { return record(row).value(0).toString(); //will fetch row at index row and get data of particular database column }
Then call this from QML using model property
table.model.getData(3) //get data at row 3
-
Wow this actually works ...
Thanks a lot p3c0 you're the best !!!Do you work for Qt or something ?