[Qt-interest] QTableView and delegate

Andre Somers andre at familiesomers.nl
Fri Nov 6 14:29:54 CET 2009


Wolfgang Pausch wrote:
> Hello Sean,
>
>   
>>> if I have a read-only QTableView with a model containing integers in some
>>> column, and want to translate them into human-readable strings before
>>> displaying them in the table, what is the best design?
>>>
>>> I first thought about using a column delegate, however I noticed that it is only
>>> called if flags() returns Qt::ItemIsEditable, which is not what I want in a
>>> read only table...  And even if flags() returns Qt::ItemIsEditable, the
>>> delegate seems to become active only if I click on the cell.
>>>
>>> I think I might get what I want by using QAbstractItemView::setIndexWidget for
>>> each cell where I want to translate ints to strings.  But this would mean that
>>> I (a) would need custom widget classes, (b) would have several extra widgets
>>> per row and (c) would have to set them manually e.g. when adding/removing rows.
>>>
>>> What do you think, is the latter really the right design, or is there some
>>> simpler solution I missed so far?
>>>   
>>>       
>> You could use a proxy model to do the translation and set that on the 
>> views where you wish to see human readable data. Or if this would be for 
>> every view, just do the translation in your model class. If you later 
>> decide to make it read/write you would have to do the reverse 
>> translation in your model's setData() function.
>>     
>
> Yes this helps.  (although I was aware of this possibility, I just hoped for one where I can avoid adding a proxy model).  
>
> By the way, what´s the reason why delegates only work when editing data?  I mean, passing the data through a delegate, calling it´s setEditorData method would be so easy and straightforward - at least for me as a user of the library...
>
>   
Delegates also work when just rendering the data. I am using that at 
several places in my code. Just reimplement the 
QAbstractItemDelegate::sizeHint() and QAbstractItemDelegate::paint() 
methods. It is to be expected that the *editor* is not used for display 
purposes... That would make for very heavy item views when you have a 
lot of data in your view. <qabstractitemdelegate.html> 
<qabstractitemdelegate.html>

André




More information about the Qt-interest-old mailing list