PDA

View Full Version : row height with QTreeView and delegates



Philipp
6th October 2008, 02:31
Hi,

I am using a QTreeView to display some data. One column is edited using a QComboBox, for which I created a delegate like in the "Spin Box Delegate (http://doc.trolltech.com/4.4/itemviews-spinboxdelegate.html)" example in the documentation. It all works great so far.
Unfortunately, the combo box's height is determined by the default row height of the QTreeView, which is too low.
I am able to add a sizeHint() method to the delegate, which is used by the view. But instead of setting a fixed height I'd like to use the sizeHint() of the combo box. The combo box is created inside createEditor() const method of the delegate, so I can't really use a class variable to store the size hint for later use. And I didn't find a method to get a an editor for a given ModelIndex either.
So am I thinking in the completely wrong direction or is there some other trick to it? I'm sure that's not the first case this has popped up, but I didn't find anything useful on Google.

Philipp

Philipp
6th October 2008, 16:48
Hm, I now did it that way:


QSize MyDelegate::sizeHint(const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
return createEditor(0, option, index)->sizeHint();;
}


This way the editor is created twice, which isn't really great but at least it works without having to use
return QSize(x,y);.

Is there an easier and better way?

Philipp

yuriry
6th October 2008, 23:38
I'm not sure if there is a better way of doing this, but your MyDelegate::sizeHint() will be leaking memory if you do not delete the editor after calling its sizeHint(). Usually view takes this responsibility but you are not returning the editor to the view.

Philipp
6th October 2008, 23:44
yuriry, thanks for the hint, that's true indeed. But still doesn't give me a better solution to my problem ;(

yuriry
7th October 2008, 00:17
Have you had a chance to play with QTreeView::uniformRowHeights, QWidget::minimumHeight and QWidget::sizePolicy (the last two are for the editor)? Sorry, I cannot provide a better hint :(

Lykurg
7th October 2008, 13:35
Hi,

I am working on a similar problem without any solution at this time.


Hm, I now did it that way:

QSize MyDelegate::sizeHint(const QStyleOptionViewItem & option,
const QModelIndex & index) const
{
return createEditor(0, option, index)->sizeHint();;
}



How do you figure out, if an editor should be provided? Is this code not returning always the same sizeHint?


Lykurg

aamer4yu
7th October 2008, 20:36
What have u set the delegate on ???
On the treeview , or the combobox ?? if on combobox, HOW ??

nifei
11th November 2008, 10:40
I have the same problem, the delegate could not return the correct sizeHint when the editors' sizes vary.