View Full Version : Propagate ItemDelegate editor widget size to the container cell?

2nd October 2009, 18:57
Hi, I am using a custom ItemDelegate with a QTreeView. The editor widget that the ItemDelegate provides is one that resizes itself dynamically, and I would like to have its size affect the size of the TreeView cell holding the widget.

I am using ItemDelegate::sizeHint() to control the size of each cell, but it appears that sizeHint() is never called when the editor widget is active, even if I explicitly emit the sizeHintChanged() signal.

Here's a snip of the relevant code:

QSize MyItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
if (_view->indexWidget(index))
// return the size hint of the active editor widget
return _view->indexWidget(index)->sizeHint();
return QItemDelegate::sizeHint(option, index);

QWidget *MyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
AutoExpandingTextEdit *editor = new AutoExpandingTextEdit(parent, index);
connect(editor, SIGNAL(autoExpanded(const QModelIndex &)), this, SIGNAL(sizeHintChanged(const QModelIndex &)));
return editor;

The AutoExpandingTextEdit is just a custom QTextEdit widget that resizes itself as the user types. Each time it resizes, it emits the autoExpanded signal. I have verified that this is successfully triggering the sizeHintChanged signal, however the sizeHintChanged signal is not causing the sizeHint method to execute when the editor is active.

Any ideas?