PDA

View Full Version : display custom widget in a TableView



code_talker
18th August 2010, 13:28
Hi,

have my own model that holds my own custom datatype in a custom role. Now I want to display the content of the model in an item view (QTableView). I read about Delegates, but I have a problem to use them correctly.

I wrote a delegate that creates a custom editor widget for my datatype using createEditor()/setEditorData().

This works, but the custom editor widget is only displayed when I doubleclick a cell to start editing.

I also want to use my custom editor widget for normal display of the items, but there is no "createViewer()" like method in QStyledItemDelegate. It seems that I have to override the paint() method of the delegate in order to influence the display of my items. But now, I'm lost. How can I get Qt to render my custom display widget in the paint() method of the delegate?

aamer4yu
18th August 2010, 13:40
I also want to use my custom editor widget for normal display of the items,
You will need to handle mouse and paint events yourself.. ie. in the paint of delegate, you will have to paint your custom widget through QStyle painter and handle the mouse / key events

Setting widget for each cell wont be a good idea.

code_talker
18th August 2010, 17:31
Thank you for this hint. It is not what I need, but it brought me on the right track. :)

I need some interaction in my custom widget, therefore painting it with the delegate is rather cumbersome. I now got it to work to just set the widgets with setIndexWidget(). This works as expected and I can guarantee, that there will be <20 items in the table, so performance should be okay.

riarioriu3
4th August 2012, 19:07
but through QItemDelegate we can only display the widget during editing the cell..
Is there any way to display it permanently ?

spirit
4th August 2012, 21:40
Well, you can use QAbstractItemView::openPersistentEditor, but it can "slow down" your view if there are a lot of data.
I would suggest to grab an editor using QPixmap::grabWidget or even better to render it using QWidget::render in the item's delegate QItemDelegate::paint method.