PDA

View Full Version : Using Designer to implement a QItemDelegate



JuanMO
3rd August 2010, 15:38
Hi All,

I'm trying to use a QListWidget to draw Items. Because the items are complex, I also create a CustomListWidgetItem and override the operator< to order them.

The problems consist when I try to draw the item. The first aproach consist in create a widget and associate them to the Item with QListWidget::setItemWidget from the list, but I think it is not correct. After looking in the forum I found out that could be better to implement a CustomItemDelegate and override paint to draw what I need.

Is there any way to use the designer output to avoid draw all manually?

Is my approach correct?

Thanks in advance

GreenScape
3rd August 2010, 19:02
the main question: what do you want to draw? if your items can be drawn just displaing Qt::DisplayRole data, then you dont need to create Delegate.

JuanMO
3rd August 2010, 19:40
Sorry, I'm currently try to understand what you meand. In addition I'll try to show what I'm trying to do.
My idea (I don't know if it is the best) is to use the render method of the widget prom the delegate::paint
Becuse the widget has to show 3 labels and an image in principle, and in the future could be change and I think that if I use the designer and someting change, I won't have to change the code.

The problem is that when I try to render the widget, I can't indicate where it should be draw
This is an example:



void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
ItemWidget item;

item.setGeometry(option.rect);

item.render(painter);
}



Regards

aamer4yu
4th August 2010, 06:39
I guess you will need to calculate that yourself.
Using a ItemWidget inside paint is quite heavy. You are literally creating and destroying a widget on every paint event !!

Proper way would be, calculate the rects of text and image you want to draw and simply draw them. dont use any widget.render() .
If more curious, you can dig into code of QItemDelegate and see how it renders the item. You will get the idea.

GreenScape
4th August 2010, 08:11
look at my example:



QLabel renderer;
renderer.setObjectName("renderer");
renderer.setStyleSheet(getStyleSheet(index));
renderer.setText(makeDisplayString(index));
renderer.resize(option.rect.size());
painter->save();
painter->translate(option.rect.topLeft());
renderer.render(painter);
painter->restore();

here i render label with my style sheet.

you must draw here:


option.rect.topLeft()

JuanMO
4th August 2010, 14:02
And what about creating only one widget which is a member of the delegate to avoid the creation of the object.

And still using the render, or is any other thing to take care about that?

Rigth now, the item is not so complicated I'll try to make it by hand. :)