Thanks again.
I'm still not quite there though. My createEditor() looks like:
{
if (index.
data().
canConvert(QVariant::Date)) { editor->setDisplayFormat("dd MMM yyyy");
// Capture the size hint for later
DateDelegate *obj = const_cast<DateDelegate *>(this);
QSize size
= editor
->sizeHint
();
QMetaObject::invokeMethod(obj,
"captureSizeHint", Qt
::QueuedConnection,
connect(editor, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
return editor;
}
else
return QStyledItemDelegate::createEditor(parent, option, index);
}
QWidget * DateDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.data().canConvert(QVariant::Date)) {
QDateEdit *editor = new QDateEdit(parent);
editor->setDisplayFormat("dd MMM yyyy");
// Capture the size hint for later
DateDelegate *obj = const_cast<DateDelegate *>(this);
QSize size = editor->sizeHint();
QMetaObject::invokeMethod(obj, "captureSizeHint", Qt::QueuedConnection,
Q_ARG(QModelIndex, index),
Q_ARG(QSize, size) );
connect(editor, SIGNAL(editingFinished()),
this, SLOT(commitAndCloseEditor()));
return editor;
}
else
return QStyledItemDelegate::createEditor(parent, option, index);
}
To copy to clipboard, switch view to plain text mode
and the custome slot captureSizeHint():
{
m_size = hint;
emit sizeHintChanged(index);
}
void DateDelegate::captureSizeHint(const QModelIndex & index, QSize hint)
{
m_size = hint;
emit sizeHintChanged(index);
}
To copy to clipboard, switch view to plain text mode
The sizeHint() returns m_size if it is valid otherwise it just hands off to the default implementation.
The code works but the column does not resize until the editor is closed. Tracing the execution I see the editor size hint being captured at creation time, but the delegate's sizeHint() method is not called again until after the editor closes. Using Qt::DirectConnection makes no difference.
Forgive me if I'm missing the blindingly obvious but I am in the middle of a steep C++ and Qt learning curve.
Bookmarks