How to get model index inside QML TreeView rowDelegate? - qt

I would like to set the height of each row in the treeview based on a model property, so I would need to have the model index to get it. But in rowDelegate, the index is not provided, only the row number, which is not correct for subitems.
https://doc.qt.io/qt-5/qml-qtquick-controls-treeview.html#rowDelegate-prop
Is there any way of getting this index, or setting the height in the itemDelegate (which has the styleData.index property)?

Related

What is the purpose of TreeView.isTreeNode

The TreeView attaches some properties to its delegate. One of them is isTreeNode. The documentation writes about isTreeNode:
required property bool isTreeNode - Is true if the delegate item represents a node in the tree. Only one
column in the view will be used to draw the tree, and therefore, only
delegate items in that column will have this property set to true. A
node in the tree should typically be indented according to its depth,
and show an indicator if hasChildren is true. Delegate items in other
columns will have this property set to false, and will show data from
the remaining columns in the model (and typically not be indented).
What is the benefit of this property? For me it looks like an alias for column == 0.
It's currently an alias for column === 0, like you say. But it makes the delegate more readable, and opens up for the possibility to rearrange columns at a later point, and improve the support for right-to-left layouts.

How to add QComboBox and TextField inside TreeView with delegate QML?

I had implement TreeView following this tutorial, But I had implement with Qt Quick Control (does not has .ui file). tutorial here. With load text and show on TreeView, it work fine. But I had another problem:
I know that a row is a TreeItem and the model is TreeModel extend from QAbstractItemModel.
- My TreeView has four columns and I set data for TreeItem with its itemData, each itemData has 4 element.
On Value Column (column 2) I want to add dynamic component (QComboBox and TextField), it depend on value of TreeItem the TreeModel. The figure below.
I set data for TreeItem and TreeModel from .cpp file, and set model for TreeView in .qml file.
I have a objectA with 8 element (name, value, data to assign component for column 2, current index of Combobox if have Combobox, data1,...) with second element maybe QStringList or QString. My TreeItem get data from objectA with TreeItem (name,value,data1,data2).
I have 3 problems need to be solve:
First problem, I had delegate the column 2 to all ComboBox or all TextField ok, But I cannot mix them together. When the value of TreeItem is a QStringList, I want delegate for Combobox, and when it is QString I want delegate for TextField. But I cannot access to the return data, I dont know that I need to define it on .cpp file or .qml file. I know that "styleData.value" is the value for a cell data, access from .qml file. But I cant to know data Type of "styleData.value" .
Second problem, Another problem that I dont know how to set current index for Combobox on load data. For example: the value(second element) of TreeItem is: [item1, item2, item3] and the current Index is at item 2.
Third problem, when data is loaded, I change data for ComboBox. How to get current Data of the TreeItem, current Index of Combobox.
Sorry for long question, and a lot of problems.
Please help me if you know any my problem.
Thank in advance.

How do I Insert an item at top of QTreeView

In my app I'd like to insert an item at the top of a QTreeView.
What I have so far will insert an item just above the currently selected item. The code (nicked, I think, from the EditableTreeviewDemo):
QModelIndex index = this->selectionModel()->currentIndex();
QAbstractItemModel *model = this->model();
if (!model->insertRow(index.row(), index.parent()))
return;
I guess what I need is the index to the current first row? How do I get this?
As a side question, what happens to the current index when a row is inserted? Does it continue to point to the same item, or the same row?
Well first you have to know that insertRow is a function from QAbstractItemModel and it will call insertRows (with an s). This function must be redefined in your model subclass if you want to allow insertion of data in your model.
http://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows
Also consider that any parent of a topmost index is a invalid QModelIndex. Then the call to do would be :
model->insertRow(0, QModelIndex());
And because this is the default value for the second parameter, simply call :
model->insertRow(0);
Then in your redefinition of insertRows simply check the validity of you parent index to ensure you news underlying data is created where you want it to be.
For you question, inserting data in the model won't affect the current and selected items.

javafx treetableview cell value not getting updated

Event after updating the data model javafx treetableview cell value not getting updated.
I am using the sample code here # http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/tree-table-view.htm
(Example 15-2)
On click of button i m trying to update first item: employees.get(0).setName("Test");
Is there any trick using which treetableview can be updated?
The example, somewhat strangely, returns a ReadOnlyStringWrapper wrapping the property values for the cell value factories. Thus instead of binding the value displayed in the column directly to the properties in the Employee class, it binds them to a new, read-only, property wrapping the value retrieved when updateItem(..) is called on the cell. This means it won't get updated when the underlying data is updated, but only if the updateItem(...) method is invoked on the cell. (I have no idea why they would do this.) So you should find, for example, that if you change the value, then collapse the root node in the TreeTableView and expand it again, that your new value is displayed after expanding the root (because this causes the cells' updateItem(...) methods to be invoked).
To make the cells update when the data is changed, bind the cell value directly to the property defined in the model (Employee) class:
empColumn.setCellValueFactory( param -> param.getValue().getValue().nameProperty());
and
emailColumn.setCellValueFactory( param -> param.getValue().getValue().emailProperty());

Qt Model View Programming: Creating indexes

I am reading through following documentation to increase my understanding of model-view programming:
http://qt-project.org/doc/qt-4.8/model-view-programming.html
Although its nicely written, I have a question regarding creating indexes. As far as I understand to calculate and item's index we need index of its parent item. To calculate index of the parent item we need index of child item. So its a circular dependency. How do you break it?
If you go through following section:
Rows and columns
The diagram shows a representation of a basic table model in which each item is located by a pair of row and column numbers. We obtain a
model index that refers to an item of data by passing the relevant row
and column numbers to the model.
QModelIndex indexA = model->index(0, 0, QModelIndex());
QModelIndex indexB = model->index(1, 1, QModelIndex());
QModelIndex indexC = model->index(2, 1, QModelIndex());
Top level items in a model are always referenced by specifying QModelIndex() as their parent item. This is discussed in the next
section.
I do not understand how does Qt know to calculate index of A, parent index should be QModelIndex() (i.e. the index of top level item).
As far as I understand to calculate and item's index we need index of
its parent item.
Yes, unless it is a top level item.
To calculate index of the parent item we need index of child item.
No. An index cannot have more than one parent, so just call QModelIndex::parent() const.

Resources