PDA

View Full Version : Creating advanced widget in a delegate.



bunjee
1st February 2007, 17:47
Hey folks,

I'm trying to display my own widget in a QListView using a QItemDelegate.

When specifying a simple widget, the delegate seems to work pretty good.
Unfortunately when I specify a more complete one, it doesn't seem to display anything.
The view is left blank, plus no resizing of the list row.



QWidget * ZeContactDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QWidget * test = new QWidget(parent);

QVBoxLayout * layout = new QVBoxLayout(test);

QPushButton * editor = new QPushButton;
QPushButton * editor2 = new QPushButton;
QPushButton * editor3 = new QPushButton;

layout->addWidget(editor);
layout->addWidget(editor2);
layout->addWidget(editor3);

test->resize(333, 333);
test->show();

return test;
}


Any thoughts ?

wysota
1st February 2007, 17:57
You shouldn't resize the widget, let the view handle the geometry. If you're trying to force item size this way, it won't work. From what I see the widget you return is hardly an editor... maybe you should use setIndexWidget() instead?

bunjee
1st February 2007, 19:01
According to the Qt documentation

void QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget )
Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.
[...]
This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.
This function was introduced in Qt 4.1.

I guess setIndexWidget might not be the best way to do that.

bunjee
1st February 2007, 19:11
I'm currently using QListView with a custom "string model" and my own delegate.

According to Qt doc :

"The QListView class provides a list or icon view onto a model."

Instead of using "QListView" I guess I could :

1/ inherit "QAbstractItemView" and create my own 'paint' routine in a new View with no delegate.
2/ "hack" the "QListView" and create my own paint routine in my delegate.

Is there a best solution?

wysota
1st February 2007, 21:03
I guess setIndexWidget might not be the best way to do that.
Depends what you want to do :)
What exactly do you want to achieve anyway? I doubt a widget with three push buttons may be appropriate to be called an "editor".

bunjee
2nd February 2007, 03:50
Something like that :

http://www.qtcentre.org/forum/f-qt-programming-2/t-skype-style-list-5409.html

A sort of expanded view of an item with several buttons and stuff when clicking on it.

Thanks.

wysota
2nd February 2007, 10:08
So why do you say setIndexWidget is not fit for it? I would myself use a custom painting delegate instead of sticking a widget there anyway, but that's me :) The thing you want to put there is certainly not an editor...

bunjee
5th February 2007, 01:07
So why do you say setIndexWidget is not fit for it? I would myself use a custom painting delegate instead of sticking a widget there anyway, but that's me :) The thing you want to put there is certainly not an editor...

If I wanted to do like you,
how would I implement a QPushButton in the painting delegate ?

Thanks.

wysota
5th February 2007, 09:09
style()->drawControl(...)
and use the editorEvent to handle a click.

Using setIndexWidget() is fine too, it'll be harder to control its looks though. I surely wouldn't use an editor widget there... There is just no benefit of doing that - you don't actually edit anything. Of course it's possible to do that and you can use it if you wish. It's just I'd chose another way :)