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.
Related
I have a code of tree view on qml:
Item {
width: 899
height: 471
TreeView {
x: 215
y: 161
width: 470
height: 150
headerVisible: true
frameVisible: false
TableViewColumn {
title: "File Name"
role: "fileName"
width: 200
}
TableViewColumn {
title: "Size"
role: "size"
width: 70
}
TableViewColumn{
title: "Last Change"
role: "lastChange"
width: 200
}
}
}
How do i add values to the red squares in the picture below?
I've been looking on youtube and documentation and hardly could understand anything that relates to that.
Define a model in TreeView.
model: ListModel {
id: fileModel
ListElement {
fileName: "myfile"
size: "1KB"
lastChange: "yesterday"
}
ListElement {
fileName: "yourfile"
size: "1MB"
lastChange: "today"
}
ListElement {
fileName: "otherfile"
size: "1GB"
lastChange: "tomorrow"
}
}
Appending row to model.
fileModel.append({
fileName: "anotherfile",
size: "1TB",
lastChange: "another day"});
Quick try
Best way is from C++. You create your model there, from scratch by subclassing from QAbstractItemModel or by filling a QStandardItemModel with nodes. Then, you make that model available to QML, for instance by setting it as a context property. Then you bind the model property of TreeView to that model from C++.
Please note that QtQuickControls 1, which features TreeView, is not generally recommended for serious use. It is quite inefficient and not really maintained any more.
In QML i have a TreeView with (properly working) multiselection:
TreeView {
id: treeview
anchors.fill: parent
model: myTestModel
selectionMode: SelectionMode.ExtendedSelection
selection: ItemSelectionModel {
model: treeview.model
}
TableViewColumn {
role: "name_role"
title: "Name"
width: 160
}
TableViewColumn {
role: "type_role"
title: "Type"
width: 75
}
}
I'd like to implement drag & drop to be able to "pull" items out of the treeview into a DropArea.
But when I use the approach I found several times, namely defining an itemDelegate that contains a MouseArea, the selection doesn't work anymore.
TreeView {
id: treeview
anchors.fill: parent
model: myTestModel
// broken due to MouseArea in itemDelegate !
selectionMode: SelectionMode.ExtendedSelection
selection: ItemSelectionModel {
model: treeview.model
}
TableViewColumn {
role: "name_role"
title: "Name"
width: 160
}
TableViewColumn {
role: "type_role"
title: "Type"
width: 75
}
itemDelegate: Item {
Rectangle {
id: rect
anchors.fill: parent
color: styleData.selected ? "blue" : "transparent"
Text {
anchors.verticalCenter: parent.verticalCenter
color: styleData.selected ? "white" : "black"
text: styleData.value
}
MouseArea {
anchors.fill: parent
drag.target: symbolAvatar
onPressed: {
var tmp = mapToItem(container, mouse.x, mouse.y);
symbolAvatar.x = tmp.x;
symbolAvatar.y = tmp.y;
symbolAvatar.dragging = true;
symbolAvatar.text = styleData.value;
}
}
}
}
}
where symbolAvatar is an item that becomes visible when a drag has started.
Any ideas how to implement drag and drop in a QML TreeView without breaking the selection?
Edit: Using the onPressAndHold event handler inside the TreeView would be a solution if I could access the mouse position there, but it doesn't seem to exist :-(
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)
}
}
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
}
}
How to access the current item in a TableViewColumn?
TableView {
id: atcTableView
model: myatclist
...
TableViewColumn {
...
}
TableViewColumn {
id: atcTableViewColFreq
role: "frequency"
title: "Frequency"
width: 120
delegate: Component {
Text {
text: "Freq is " + currentItem / model / model.frequency
}
}
}
As of this similar question " How do you access the roles of the currentItem from a listview in QML? " I have tried all kind of combinations model, modelData , currentItem, and something like model.role.
If I remove the delegate entirely, frequency displays correctly. Model is based on QAbstractListModel. Any hints?
Btw, can I see in QML debugging what properties are available in a delegate?
-- Edit based on Kakadu's comment --
delegate {
Text {
text: "freq is " + frequency
}
}
gives me: ReferenceError: frequency is not defined
delegate: Text { text: view.model.get(styleData.row).frequency }
Yow must to define the role in te QAbstractItemModel.
QHash YourClassModel::roleNames() const {
roles[Qt::UserRole + 1] = "frequency";
return roles;
}
Try to use styleData.value:
delegate: {
Text { text: styleData.value }
}
It's described here (look for itemDelegate property)