// Starting point: shot0.png
// I type 'b' into the current cell
// result: shot1.png
// the cell does not display as an active editor, no completer visible: middle screen shot
// type 'a'
// result: shot3.png
// setEditorText() called with 'b' but active editor displaying 'a', no completer popup
// Starting point: shot0.png
// I type 'b' into the current cell
CompleterDelegate::createEditor() called for QWidget(0x83582e0, name = "qt_scrollarea_viewport") QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(, )
CompleterDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) )
CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(, )
CompleterDelegate::setModelData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(, )
CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(QString, "b")
// result: shot1.png
// the cell does not display as an active editor, no completer visible: middle screen shot
// type 'a'
CompleterDelegate::createEditor() called for QWidget(0x83582e0, name = "qt_scrollarea_viewport") QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(QString, "b")
CompleterDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) )
CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9858a4) ) QVariant(QString, "b")
// result: shot3.png
// setEditorText() called with 'b' but active editor displaying 'a', no completer popup
To copy to clipboard, switch view to plain text mode
Shot3.png is the behaviour expected and seen if you explicitly enter edit mode.
#include <QtGui>
#include <QDebug>
class CompleterDelegate : public QStyledItemDelegate
{
public:
CompleterDelegate
(QObject* parent
= 0) : QStyledItemDelegate
(parent
) { }
QWidget* createEditor
(QWidget* parent,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
qDebug() << "CompleterDelegate::createEditor() called for" << parent << index << index.data(Qt::EditRole);
completer
->setCompletionMode
(QCompleter::PopupCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
lineEdit->setCompleter(completer);
return lineEdit;
}
{
qDebug() << "CompleterDelegate::setEditorData() called for" << index << index.data(Qt::EditRole);
QStyledItemDelegate::setEditorData(editor, index);
}
{
qDebug() << "CompleterDelegate::setModelData() called for" << index << index.data(Qt::EditRole);
QStyledItemDelegate::setModelData(editor, model, index);
}
{
qDebug() << "CompleterDelegate::updateEditorGeometry() called for" << index;
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
}
};
int main(int argc, char* argv[])
{
tv.setWindowTitle("View");
// default edit triggers are:
tv.setModel(&model);
tv.setItemDelegate(new CompleterDelegate(&tv));
tv.show();
return app.exec();
}
#include <QtGui>
#include <QDebug>
class CompleterDelegate : public QStyledItemDelegate
{
public:
CompleterDelegate(QObject* parent = 0) : QStyledItemDelegate(parent) { }
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
qDebug() << "CompleterDelegate::createEditor() called for" << parent << index << index.data(Qt::EditRole);
QLineEdit* lineEdit = new QLineEdit(parent);
QStringList stringList = QStringList() << "foo" << "bar" << "baz";
QCompleter* completer = new QCompleter(stringList, lineEdit);
completer->setCompletionMode(QCompleter::PopupCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
lineEdit->setCompleter(completer);
return lineEdit;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const
{
qDebug() << "CompleterDelegate::setEditorData() called for" << index << index.data(Qt::EditRole);
QStyledItemDelegate::setEditorData(editor, index);
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
qDebug() << "CompleterDelegate::setModelData() called for" << index << index.data(Qt::EditRole);
QStyledItemDelegate::setModelData(editor, model, index);
}
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug() << "CompleterDelegate::updateEditorGeometry() called for" << index;
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
}
};
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QStandardItemModel model(4, 2);
QTableView tv;
tv.setWindowTitle("View");
// default edit triggers are:
tv.setEditTriggers(QAbstractItemView::DoubleClicked
| QAbstractItemView::EditKeyPressed
| QAbstractItemView::AnyKeyPressed );
tv.setModel(&model);
tv.setItemDelegate(new CompleterDelegate(&tv));
tv.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks