PDA

View Full Version : QStyledItemDelegate : too small to see contents



Everall
9th December 2010, 11:25
QStyledItemDelegate does a nice job and renders the item using the correct size of the items contents. But when I start editing then the spinbox decoration hides the contents. I’d like to change this behavior so that the content is always visible.(see image 1)

I subclassed QStyledItemDelegate::updateEditorGeometry to change the width of the editor

QSize ExtraSize= QSize(option.rect.width()+ option.decorationSize.width(),option.rect.height() );

QRect ExtraRect = option.rect;
ExtraRect.setSize(ExtraSize);
editor->setGeometry(ExtraRect);
But this doesn’t give the expected results. When debugging I see that decorationSize remains the same, even when using larger fonts.
With QFont fff("Arial",12);

debugging output : option.decorationSize.width() 16
(see image 2)



With QFont fff("Arial",32);

debugging output : option.decorationSize.width() 16
(see image 3)


QUESTION : Can anyone tell me which approach I should use to get the correct size of the decoration?
Or should I make a custom style instead of using delegates?

wysota
9th December 2010, 14:31
I would put the question differently - what is the effect you want to obtain and what is the reason for wanting to have this effect?

Everall
9th December 2010, 19:17
Hi Wysota,

What I want : When I use a QTableView and edit an item the spinbox decoration doesn't hide the content of the time cell. As I understand, this is the default behaviour. Tell me if I'm wrong. I would prefer to have the content always visible, editing or not.

I tested the different text sizes on windows 7 (100%, 125% and 150%) without hardcoding it with setfont in my program. In that case my code gives what I want : a spinbox on the right of the cell frame and minimal use of space.(see image)

Conclusion : option.decorationSize.width() adapts to the font size of the Operating system and not to the font size i choose manually in my code with setFont.

You can see the default editing and my result in the attached image.
5586

Cheers Everall

PS : if somebody wants an example I can send the complete code.

wysota
9th December 2010, 19:30
There is no such thing as "spinbox decoration". The "decoration" of an item is an icon that has nothing to do with the widget performing the role of an editor for an item.

I'm not sure I understand what the problem is - you mean you want the cell to become wider because as it is now it doesn't fit the whole time format when in edit mode, correct? If so, then you have to return a larger size hint for your item as it is the editor that adjusts to the cell size, not the other way round. You should be able to do that by emitting QAbstractItemDelegate::sizeHintChanged() from the delegate when the editor is created and later return a larger size hint when the view asks the delegate for it.

Everall
9th December 2010, 20:38
Hi Wysota,

You're right : obviously decoration is something else than what I thought. But it gives a result that suits me. So the luck was on my side.
;)

Nevertheless, I prefer to program as QT is intended to be used, so I will have a look into your approach.

Thanks for your insight and the sizeHintChanged() tip.

Everall