问题
I am creating a TableView with custom delegate to have checkboxes in the columns and I have had some success with the following:
// TableViewCheckBoxColumn.qml
import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
TableViewColumn {
title: ""
role: "check"
delegate: CheckBox {
id: checkBox
}
}
The table implementation is as follows:
import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
Item {
anchors.centerIn: parent
ListModel {
id: testModel
ListElement {
check1: true
check2: true
check3: false
check4: false
check5: false
}
}
TableView {
anchors.centerIn: parent
TableViewCheckBoxColumn {
id: checkedColumn1
}
TableViewCheckBoxColumn {
id: checkedColumn2
}
TableViewCheckBoxColumn {
id: checkedColumn3
}
TableViewCheckBoxColumn {
id: checkedColumn4
}
TableViewCheckBoxColumn {
id: checkedColumn5
}
model: testModel
}
}
This, at least, creates the TableView with the 5 columns with checkboxes in each of them. However, I cannot figure out how to propagate the checked statuses of the columns from my testModel to the TableView.
回答1:
Ok first you need to match the role property of the columns with the property of the the model. Then you can access to the value of the property using the property 'styleData. value'
ListModel {
id: testModel
ListElement {
check1: true
check2: true
check3: false
check4: false
check5: false
}
}
TableView {
width: 1000; height: 500
anchors.centerIn: parent
TableViewColumn {
role: "check1"
delegate: CheckBox {
checked: styleData.value
}
}
TableViewColumn {
role: "check2"
delegate: CheckBox {
checked: styleData.value
}
}
TableViewColumn {
role: "check3"
delegate: CheckBox {
checked: styleData.value
}
}
TableViewColumn {
role: "check4"
delegate: CheckBox {
checked: styleData.value
}
}
TableViewColumn {
role: "check5"
delegate: CheckBox {
checked: styleData.value
}
}
model: testModel
}
This is better:
TableView {
width: 1000; height: 500
anchors.centerIn: parent
TableViewColumn {
role: "check1"
}
TableViewColumn {
role: "check2"
}
TableViewColumn {
role: "check3"
}
TableViewColumn {
role: "check4"
}
TableViewColumn {
role: "check5"
}
model: testModel
itemDelegate: CheckBox {
checked: styleData.value
}
}
来源:https://stackoverflow.com/questions/35853791/using-checkboes-in-tableview