PDA

View Full Version : QListView and delegate to display data



T4ng10r
7th May 2007, 13:59
First - i've made ListView with some names.
Then I wish that every time user selects name - this position expands to my widget with more data (from other structure - name would be key - or maybe you know better way?).
As I see in QItemDelegate - it should work to display and edit data. Theres a place where widget for editing is created - but there's no place where to create widget for display. At last i see nowhere.
Other approach - connect my self to clicked() signal and then set my widget to appropriate cell.

marcel
7th May 2007, 14:08
I suggest using QListWidget, and subclassing QItemDelegate and QListWidgetItem.

In the subclass of QListWidgetItem you add roles for the info you wish to draw and override
QVariant data( int role ) const;

In the subclass of QItemDelegate you must only override paint(...) in which you paint the item, and sizeHint() where you return the sizeHint of you item.

It is more flexible this way.

regards

gauravagarwal
7th May 2007, 14:08
Hi

while using QListView, custom widgets are displayed as an QListView item while editing only. ie when u double click item to edit its content (see spinboxdelegate example in Qt manual).

To display data as per ur need in as QListView items, use model and delegates both.
Use delegate paint() method to paint items in QListView as you needed.

U have to use QModelIndex for this.
give it a try, it will definetely work.

Regards
Gaurav Agarwal

jpn
7th May 2007, 15:40
I'd simply adjust the edit triggers (http://doc.trolltech.com/4.2/qabstractitemview.html#editTriggers-prop) of the view. Either QAbstractItemView::CurrentChanged or QAbstractItemView::SelectedClicked, however you want it to be. Then, all you need to do is to provide a bigger size hint and a custom widget as the editor.

T4ng10r
7th May 2007, 19:32
Custom widgets are drawn correctly but, don't know why, place for each line isn't expanding. Which means that my widget is drawn over 3 string lines.
I've added sizeHint appropriate for my widget, but edited line isn't expanding. What should I do to make it look correct?

T4ng10r
30th May 2007, 10:25
To make editor-widget proper size we need to set SizeHintRole in index for which we create this editor.
But this has to be done in delegate createEditor method - not in setEditorData. SetEditorData is being called every time model changes.
To make sure that editor-widget overwrites background we need to setAutoFillBackground(true) for him.

Problem lies also - where to put SizeHintRole reset for removed editor?
In my opinion - in onCloseEditor slot connected with closeEditor signal from QAbstractItemDelegate looks promising.