Hi All,
I want to add one custom widget having a label and a button to my listview as one list item ...
If there is any code snippet or example code in this scenario .. please do suggest ..
Thanks,
Amit
Hi All,
I want to add one custom widget having a label and a button to my listview as one list item ...
If there is any code snippet or example code in this scenario .. please do suggest ..
Thanks,
Amit
Use void QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
See also the warning in the documentation that you have to create a delegate if you want anything sensible with the widget. Generally, adding widgets to an itemview is a bad idea because it does not scale. If you 1000 rows, you need 1000 widgets in a naive implementation. The better implementation is to have only as much widgets as currently visibie and reuse widgets that get out of view for the appearing rows during scrolling. Basically, that is what the delegate does.
It's nice to be important but it's more important to be nice.
MarkoSan (10th January 2010)
If you want to show only standard controls like labels, buttons, check boxes then the most efficient way is to make your own item delegate where in paint() method you paint those controls with QStyle (see drawControl() and others). But those controls are only drawn so you have to implement by hand event processing in QAbstractItemDelegate::editorEvent() and you have to make eventFilter and install it on view's viewport to which you have to enable mouse tracking (and maybe set WA_Hover attribute to get Leave and Enter events - I don't remember exactly). Sounds complicate but it is the most efficient way in case with simple widgets, because you don't really have any real widget objects.
I would like to be a "Guru"
Useful hints (try them before asking):
- Use Qt Assistant
- Search the forum
If you haven't found solution yet then create new topic with smart question.
MarkoSan (10th January 2010)
I guess the widget replaces the delegate for that certain cell. If you have 100 rows, you will have to set 100 widgets. I think you will also have to to data binding of that widget yourself.
It's nice to be important but it's more important to be nice.
Delegate is responsible for drawing the whole cell's content including labels. It is also responsible for the behaviour.
It's nice to be important but it's more important to be nice.
So, I subclass QWidget with all labels, pic, buttons, ..., layout them as in attachment and then I set this widget to subclassed QAbstractItemDelegate?
Qt 5.3 Opensource & Creator 3.1.2
Yes but there is one showstopper: The delegate will use the widget only in edit state. For all other cells, it will use QAbstractItemDelegate:aint.
It's nice to be important but it's more important to be nice.
No: For example, if you have a table, you see a lot of cells, but only one cell can show a blinking textcursor at the same time. This cell is in "editing" state. All others are just "labels". You usually not use full widgets for the cells that are not in editing state because of performance reasons.
It's nice to be important but it's more important to be nice.
MarkoSan (10th January 2010)
Well, thanks, but that means my cell design will be performance demanding??
Qt 5.3 Opensource & Creator 3.1.2
Bookmarks