How to access Data of a Column in a TreeView with Index - qt

I want to access Data of a Column in a TreeView while knowing Index but I have trouble to do it.
Here is an example. I want to change the value of isCollapsed when a row is expanded:
TreeView {
id: view
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
width: 100
delegate : MouseArea {
id:mous
property bool isCollapsed : false
}
}
model: fileSystemModel
onExpanded {
index.data("filePermissions").isCollapsed = true //This is not working, I don't find the right syntaxe
}
}

Related

Change property of delegate on signal

I have a TreeView, and on the signal onSortIndicatorChanged I would like to set the checked property of a CheckBox inside the delegate of a TableViewColumn in my TreeView. However, I don't know how to do this.
Component {
id: mycomp
Item {
id: myitm
CheckBox {
id: mycbx
checked: true
}
}
}
TreeView {
TableViewColumn {
delegate: myDelegate
}
onSortIndicatorChanged{
// set the checked property of the delegates to true
}
}
You could try to add a property checked to your tree view and bind the check boxes to this property instead. Then, in the even handler, just set the value of the TreeView's property:
Component {
id: mycomp
Item {
id: myitm
CheckBox {
id: mycbx
checked: view.checked
}
}
}
TreeView {
id: view
property bool checked: false
TableViewColumn {
delegate: mycomp
}
onSortIndicatorChanged {
view.checked = true
}
}
Update:
In case you want to set a per-item checked state, you can use a similar approach where you store a function as a property of the view. In the delegate, you could call this stored function to get an item specific property value:
Component {
id: mycomp
Item {
id: myitm
CheckBox {
id: mycbx
checked: view.checkFunction ? view.checkFunction(modelData) : true
}
}
}
TreeView {
id: view
property var checkFunction: null
TableViewColumn {
delegate: mycomp
}
onSortIndicatorChanged {
view.checkFunction = function(modelData) {
// Calculate the checked state based on the modelData
return modelData.foo == "bar";
}
}
}

QML Tableview display value from selected row

I have a tableview and I want to open a dialog box on onPressAndHold on a row and display the value of the cell of the row "orderNumber".
But i get the Error message: ReferenceError: row is not defined
TableView {
id: tableviewopenorders
height: 180
clip: false
visible: true
onPressAndHold: oocanceldialog.open()
TableViewColumn {
id: orderNumberColumn
role: "orderNumber"
title: "Order Number"
}
model: openordersModel
}
ListModel {
id: openordersModel
ListElement {
orderNumber: "1223455"
}
ListElement {
orderNumber: "111111"
}
}
Dialog {
id: oocanceldialog
title: "Cancel confirmation"
standardButtons: Dialog.Ok | Dialog.Cancel
x: (parent.width - width) / 2
y: (parent.height - height) / 2
Label {
text: openordersModel.get(row).orderNumber
}
onAccepted: console.log("Ok clicked")
onRejected: oocanceldialog.close()
}
row exists in the context of onPressAndHold, so it does not exist outside of it, to get the row we must use the currentRow attribute of the TableView:
currentRow : int
The current row index of the view. The default value is -1 to indicate that no row is selected.
In your case:
Label {
text: openordersModel.get(tableviewopenorders.currentRow).orderNumber
}

How to get currentIndex of an delegateItem in a TreeView

Hellow,
When using delegate on a Column, how could I know the index of the Cell or Row selected if the user clicks on the delegateItem ?
Here is an example. The second Column is a MouseArea and I want to expand the currentIndex when the user clicks on the MouseArea:
TreeView {
id: view
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
width: 100
delegate : MouseArea {
id:mous
onClicked {
//get indexMouseArea
view.expand(indexMouseArea)
}
}
}
model: fileSystemModel
onExpanded {
console.log("expanded :" + index)
}
}
Solution thanks to #mcchu:
Hellow,
When using delegate on a Column, how could I know the index of the Cell or Row selected if the user clicks on the delegateItem ?
Here is an example. The second Column is a MouseArea and I want to expand the currentIndex when the user clicks on the MouseArea:
TreeView {
id: view
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
width: 100
delegate : MouseArea {
id:mous
onClicked {
view.expand(styleData.index);
}
}
}
model: fileSystemModel
onExpanded {
console.log("expanded :" + index)
}
}

How to know when a TreeView Node is closed ( not Expanded)

I found in the doc TreeView doc that you can use onExpanded to know when a Node from a TreeView is expanded but how can you know when a Node is closed ?
Code example:
TreeView {
TableViewColumn {
title: "Name"
role: "fileName"
width: 300
}
TableViewColumn {
title: "Permissions"
role: "filePermissions"
width: 100
}
model: fileSystemModel
onExpanded {
console.log("expanded :" + index)
}
}
From the documentation, onCollapsed is the opposite of onExpanded, so you can check when that signal is emitted.

TableView not the correct size with QML

I am trying to create a TableView with QML where I have a checkbox, an image and a text field. The table column definitions are as follows:
// TableViewCheckBoxColumn.qml
TableViewColumn {
title: ""
role: "check"
delegate: CheckBox {
anchors.fill: parent
checked: styleData.value
}
}
//TableViewImageColumn.qml
TableViewColumn {
title: ""
role: "thumbnail"
delegate: Image {
anchors.fill: parent
source: styleData.value
width: 30
height: 30
}
}
Now the data model and the table itself is defined as a QML component as follows:
Item {
ListModel {
id: sourceModel
ListElement {
check: false
thumbnail: "file:///Users/xargon/alignment.png"
length: "10:22"
}
}
// Table view
TableView {
anchors.centerIn: parent
alternatingRowColors: false
TableViewCheckBoxColumn {
id: checkedColumn
}
TableViewImageColumn {
id: thumbColumn
}
TableViewColumn {
id: lengthColumn
role: "length"
title: "Length"
}
model: sourceModel
}
}
Now, this is embedded in a ColumnLayout and a StackView as:
ColumnLayout {
anchors.fill: parent
MyTable {
id: reviewScreen
Layout.fillWidth: true
}
StackView {
id: options
Layout.fillHeight: true
Layout.fillWidth: true
initialItem: reviewScreen
}
}
Now I was expecting the table to fill the entire width of the parent control and I also was expecting the image to be drawn as a 30 x 30 image but what I see is the attached screenshot where the horizontal scrollbar is there to move between controls and the table is small and the image is very distorted as well.
yes, only you need declare heigh and weight of every TableViewColumn, for example:
TableViewColumn { id: lengthColumn; role: "length"; title: "Length"; height: parent.height/8; width:parent.width*0.25}

Resources