PDA

View Full Version : Should I implement an own version of QItemDelegate for QTreeView?



pir
11th May 2006, 23:18
Hi!

I am new to Qt am making a QTreeView displaying a list of different objects. I want to be able to double click one of them and change it's name. Preferably, when the object is double clicked, the old name should be selected so the user can delete it by start write the new name and press enter.

I have looked around and can't really decide what to do... should I implement a class inheriting QItemDelegate? Or is this functionality already implemented?

I tried to use setModelData() but got the error:
"property("text") failed: property invalid or does no exist."

When reading the Model/View introduction on Trolltechs homepage I get the impression that there already are a lot of functionality implemented, but when I look at the documentation for the setEditorData() it says that it does nothing in the base implementation. So I'm a bit confused. I am a bit tired of finding out that the functionality already exist when I made a own implementation, so I thought that it would be a good idee to look around first but I can't find much about this.

Any tips or examples to share?


/pir

wysota
11th May 2006, 23:57
It's all there. All you need to do is to set appropriate edit triggers in the tree (QAbstractItemView::DoubleClicked in your case). And of course the model should allow editing (QAbstractItemModel::setData() should be implemented).

pir
12th May 2006, 15:21
Thanks!

When I double click the object I want to change, the string can be changed to another. But the text is not highlighted... you know when you mark a text with the mouse. How do I make the text highlighted when I double click the text to start editing?

/pir

jpn
12th May 2006, 19:31
When I double click the object I want to change, the string can be changed to another. But the text is not highlighted... you know when you mark a text with the mouse. How do I make the text highlighted when I double click the text to start editing?
I think you might have to reimplement QItemDelegate::setEditorData() for this.
First, call the base class implementation which sets the correct data in the editor for you.
Then cast the editor widget to a line edit and call QLineEdit::selectAll().