PDA

View Full Version : Type information in QML TableView



sedi
23rd November 2015, 00:04
I have a QML TableView which is dynamically setting up its columns according to a c++ model. I want to use different column delegates for different types. Is there any possibility to get the typeName of a model property from within QML? I have a workaround by using a typeName provided (as a string) by my model, but I'd rather directly have the typeName from QML. Is that possible at all?


Here's my code so far:

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQmlTricks 1.0
import "qrc:/import/QtQmlTricks/" as QmlTricks



TableView {
// model: must be bound to an external model, which has to be properly filled before TableView is created
model: mainWindow.mainWindowTableModel
id: dynamicTableViewBasis
property var tableModel: dynamicTableViewBasis.model
signal clickedEntry(var id, string role, var value)

width: parent ? parent.width : 0

resources:
{
var roleList = dynamicTableViewBasis.tableModel.userRoleNames
var temp = []
for(var i=0; i<roleList.length; i++)
{
var role = roleList[i]
if (!dynamicTableViewBasis.tableModel.isColumnHidden( role)) {
var columnHeaderName = dynamicTableViewBasis.tableModel.columnHeaderName( role)
if (columnHeaderName === "") {
columnHeaderName = role
}
if (dynamicTableViewBasis.tableModel.columnTypeName(r ole) == "bool") {
temp.push(columnComponentBool.createObject(dynamic TableViewBasis, { "role": role, "title": columnHeaderName, "width": dynamicTableViewBasis.tableModel.columnInitialWidt h(role)}))
} else {
temp.push(columnComponentText.createObject(dynamic TableViewBasis, { "role": role, "title": columnHeaderName, "width": dynamicTableViewBasis.tableModel.columnInitialWidt h(role)}))
}
}
}
return temp
}

Component {
id: columnComponentText
TableViewColumn {
delegate: Text {
id: textDelegate
text: styleData.value ? styleData.value : ""
color: styleData.textColor ? styleData.textColor : ""
elide: styleData.elideMode ? styleData.elideMode : ""

MouseArea {
anchors.fill: parent
onClicked: {
var id = dynamicTableViewBasis.tableModel.data(dynamicTable ViewBasis.tableModel.index(styleData.row,0),dynami cTableViewBasis.tableModel.roleForName("myId"))
var role = dynamicTableViewBasis.getColumn(styleData.column). role
var value = styleData.value
dynamicTableViewBasis.clickedEntry(id, role, value)
}
}
}
}
}
Component {
id: columnComponentBool
TableViewColumn {
delegate: CheckBox {
id: checkBoxDelegate
text: ""
checked: styleData.value ? styleData.value : undefined
onClicked: {
var id = dynamicTableViewBasis.tableModel.data(dynamicTable ViewBasis.tableModel.index(styleData.row,0),dynami cTableViewBasis.tableModel.roleForName("myId"))
var role = dynamicTableViewBasis.getColumn(styleData.column). role
var value = checkBoxDelegate.checked
dynamicTableViewBasis.clickedEntry(id, role, value)

}
}
}
}
}

anda_skoa
23rd November 2015, 07:41
You could try the JavaScript typeof operator.

Cheers,
_

sedi
23rd November 2015, 09:53
Thank you, that was exactly what I was looking for! Unfortunately typeof does not differentiate too much between types - it's java-after all. A QDate is just an "object", as is a QTime. So I will probably stick to the manual type information approach via c++. But I will definitely need the typeof operator in other contexts.
Thanks a lot!