PDA

View Full Version : correct sizeHint of QItemDelegate



nifei
11th November 2008, 10:32
I subclassed QItemDelegate to create some custom/standard labels in the view and open them persistent. the labels' size changes from time to time, depending on the model's data. i wonder how to return a correct sizeHint so that the view knows what size should be assigned to each item. i did like this, obviously, it is not a good way to calculate the right sizeHint, and it doesn't return right size when the data changes.

QSize LabelDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index)const
{
UnselectedLabel *label = new UnselectedLabel(index.data().toString());
QString str = index.data().toString();
label->adjustSize();
QSize sizeHint = label->size();
return sizeHint;
}

Any help will be appreciated, thank you.

spirit
11th November 2008, 10:38
try to reimplement QItemDelegate::updateEditorGeometry in such way


void MyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}

nifei
12th November 2008, 01:20
try to reimplement QItemDelegate::updateEditorGeometry in such way


void MyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}


Thank you. i did just as you said.However, it seems that the updataEditorGeometry cannot change the item's position, it just change the widget's position and size. the items are still not high enough. once the widgets' sizes change, they are placed the same position, that just look like this: the 2ed picture shows the overlapping labels, since the 1st label's height increased and the delegate didnot change the size of an item.

spirit
12th November 2008, 06:25
you can change width and height of item using QHeaderView::defaultSectionSize (http://doc.trolltech.com/4.4/qheaderview.html#defaultSectionSize-prop)

nifei
17th November 2008, 09:50
Thank you. i'll try.

but my view is supposed to hide it's horizontal & vertical headers.