[Interest] ItemDelegate and word wrapping

André Somers andre at familiesomers.nl
Fri Feb 1 11:27:38 CET 2013


Op 1-2-2013 10:57, Bache-Wiig Jens schreef:
> On Feb 1, 2013, at 10:16 AM, André Somers <andre at familiesomers.nl> wrote:
>
>> Op 1-2-2013 9:43, Bache-Wiig Jens schreef:
>>> On Feb 1, 2013, at 8:52 AM, Иван Комиссаров <abbapoh at gmail.com> wrote:
>>>
>>>> Hello. I'm trying to implement a delegate with QTextOption::WrapAtWordBoundaryOrAnywhere. I copied QItemDelegate and replaced QTextOption::WordWrap where needed. Also i made small fix for sizeHint() calculation. Everything is ok, except that QItemDelegate doesn't look native:)
>>> I don' know if it will resolve this particular issue, but QItemDelegate was superseded by QStyledItemDelegate for exactly that reason. I would suggest trying that instead.
>>>
>>> http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html
>>>
>> Actually, I found that QStyledItemDelegate makes it even _harder_ to do
>> this. Where QItemDelegate had a range of virtual protected functions
>> that were responsible for drawing part of the delegate (so you could
>> easily opt to only re-implement one and simply re-use the rest)
>> QStyledItemDelegate doesn't help you with the rendering of these kinds
>> of elements at all. You're just left to your own devices, using ugly
>> hacks to get the base implementation to do part of the rendering for you.
> You are right in that they both have their uses so calling it "superseded" was inaccurate. They are both available in the API. If you truly want to customise the entire delegate, including the branch indicators and selection color / behavior, QItemDelegate is what you want.
>
> On the other hand if you primarily want to respect the native look and feel or a style sheet, you should try to use QStyledItemDelegate. It gives you less control but generally looks more native. The main difference is that you then only get to control the text or icon layout and/or the editor widget. In this particular case it sounds like this is what the user needs.
>
That's the point: you can't even make simple modifications like that. At 
least, I don't see it. I'm curious how you are going to change the 
layout and rendering of the text and icon using QStyledItemDelegate, and 
keep the standard look for the rest of it? I've used the model view 
stuff in Qt quite a lot, and I don't see it. Not without using an ungly 
hack.

The hack I used was that my QStyledItemDelegate-derived delegate 
actually contains a model like this:
QStringList list;
list << QString();
QStringListModel* fakeModel = new QStringListModel(list);

Then, in my paint method, I render my content and instead of calling the 
base class implementation with the same arguments as my own paint method 
was called with, I replace the model index argument with
fakeModel->index(0,0);

Otherwise, I see no easy way to convice QStyledItemModel that I will 
take care of rendering the contents of the cell, as long as 
QStyledItemDelegate takes care of rendering the rest. And my contents I 
mean more than just the text. For just the text, you can reimplement 
displayText to return an empty string. Not so for images, as far as I see.

However, I am very curious how you would do this. You're much, much more 
familiar with the style and rendering code than I am, I may be 
overlooking the obvious here...

André




More information about the Interest mailing list