Meloun
Meloun

Reputation: 15099

PyQt: how to edit item in itemmodel?

I have QStandardItemModel in a tree and I need to edit some item. I need make the same action, what user can do by double click.

Items are editable.

Upvotes: 1

Views: 2032

Answers (1)

leemes
leemes

Reputation: 45725

To start editing of an item, you need call this slot of the view (not the model!):

myView.edit(index);

You can also set the new value directly via QAbstractItemModel.setData. The role for this defaults to Qt.EditRole which is the same role used by the views when editing has been finished:

myModel.setData(index, newValue);

where index references the item you want to edit.

You can create such a QModelIndex by asking the model:

myModel.index(row, column); # for a root item
myModel.index(row, column, parent); # for a children of "parent"

So, for example, if you want to set the third item in the second root item to "foo", write:

index1 = myModel.index(2, 1);
index2 = myModel.index(3, 1, index1);
myModel.setData(index2, "foo");

Upvotes: 2

Related Questions