PDA

View Full Version : QStandardItemModel principle



bmn
1st March 2016, 14:31
Hello,

I am currently trying to figure out how to structure my widget and probably need a little help. This is not about a detailed implementation.

I have data that needs to be displayed in a QTreeView. It contains two rows, and is 2 levels deep.
The second row should be able to contain a QLabel, QLineEdit, QCheckBox or a QComboBox.


invisibleRootItem
--> ParentItem1
--> Child 1 QCheckBox
--> Child 2 QLabel
--> Child 3 QLineEdit
....
--> ParentItem2
--> ParentItem3

However, I am not completely sure how to access individual items and the state of their 2 row (QCheckBox, QLineEdit...).
There will be about 100 QStandardItems in the Model, and it feels like storing a pointer to each one is not the solution.
So how can I clearly identify the individual QStandardItem within the model to set the parameters in the second row, or read their content?

Thank you in advance.

bmn

d_stranz
1st March 2016, 16:08
I think you are confusing the model with the visualization of the model in a tree view. It is the QAbstractItemView that supports displaying QWidget instances (as delegates) when editing the view of the model.

However, if what you want is to be able to display a checkbox, then use QStandardItem::setCheckable(). To allow the text to be edited in a QLineEdit equivalent, use QStandardItem::setEditable(). A label is just a QStandardItem with editable set to false.

You can set these flags at any time, but it is usually done when items are added to the model.

bmn
1st March 2016, 19:27
That does not answer my main question of how to keep track of the items.

I also need a QComboBox, therefore I would need the delegate to view it as such, right?

ChrisW67
1st March 2016, 20:19
Normally, the widget used by an item view to edit data in the underlying model is created as required when editing mode is entered on a cell. The delegate creates the widget as editing is started, and deletes the widget when editing is finished. When the cell is not being edited, the delegate is responsible for the static display of the data. The default delegate is very basic, edits with a QLineEdit and displays the Qt::DisplayRole. You do not need to keep track of editing widgets between edits.

You can supply a delegate that does different things for different data types, indexes in a model, columns or rows in a view, or edits one item dependent on other items etc. Your custom delegate can display the data any way it sees fit when it is not being edited, including faking the appearance of the editing widget.