PDA

View Full Version : how to suppress model::data DecorationRole when the QTableView cell editor is active



davidb
14th October 2010, 14:26
I have a delegate to create a QLineEdit control when you want to edit a cell in my subclass of QTableView. In the data function of my model, I recently added a case to return an icon for Qt::DecorationRole for some items.

As the user edits a cell that has an icon, the value they enter may cause the icon to disappear. That is all working correctly.

The problem is, if the icon disappears as the user is still typing in the cell, my QLineEdit control is still sized as if there were an icon in the cell, but since there is no longer an icon, part of the text that the user is typing is displayed where the icon used to be.

So, I'd like to have my delegate size the QLineEdit editor to fill the whole cell even when an icon is present (so the icon would be invisible when editing), or even better I think, have the delegate suppress anything returned for Qt::DecorationRole when editing.

Currently, my delegate has these functions:

QWidget *MapTextDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & /*option*/,
const QModelIndex & /*index*/) const {
QLineEdit *line_editor;
line_editor = new QLineEdit(parent);
connect(line_editor, SIGNAL(textChanged(QString)),
this, SLOT(MapTextChanged()));
return line_editor;
}

QSize MapTextDelegate::sizeHint(const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
QLineEdit *line_editor = new QLineEdit();
return line_editor->sizeHint();
}
I don't see anything else in the delegate that might have to do with the size of the editor. I'm not too familiar with how to use delegates - actually, I'm pretty new to C++ and Qt.

Any ideas? I'm using Qt 4.7.

davidb
15th October 2010, 22:30
Turns out to be very easy. I reimplemented updateEditorGeometry in the delegate as follows, and that took care of it!


void MapTextDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
editor->setGeometry(option.rect);
}