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)
}
}
Related
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
}
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 :-(
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.
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
}
}
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}