PDA

View Full Version : QCompleter behvaiour in table view



ChrisW67
7th July 2011, 01:39
I have a similar problem to the one back in this thread although the code is C++ (below) not Python. A QLineEdit with completer in a table view only displays expected completion behaviour under some circumstances.

If you double-click or enter edit mode explicitly (F2 on Linux) the completer works correctly on the cell. If you trigger edit mode by just typing with a current cell selected this is the debug output:


// 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(0xbf9 858a4) ) QVariant(, )
CompleterDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9 858a4) )
CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9 858a4) ) QVariant(, )
CompleterDelegate::setModelData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9 858a4) ) QVariant(, )
CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9 858a4) ) 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(0xbf9 858a4) ) QVariant(QString, "b")
CompleterDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9 858a4) )
CompleterDelegate::setEditorData() called for QModelIndex(0,0,0x8359818,QStandardItemModel(0xbf9 858a4) ) QVariant(QString, "b")
// result: shot3.png
// setEditorText() called with 'b' but active editor displaying 'a', no completer popup

Shot3.png is the behaviour expected and seen if you explicitly enter edit mode.
6639

The cycle through setEditor, setModel, the setEditor at 5-7 of the debug out is telltale. Wysota mentioned something about focus and event filters in the old thread. I have briefly (minutes not hours) looked at the find completer stuff in Qt Creator but did not find event filter actions related to focus on arbitrary key presses.

Can anyone shed light on how to get correct behaviour here?



#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::DoubleClicke d
| QAbstractItemView::EditKeyPressed
| QAbstractItemView::AnyKeyPressed );
tv.setModel(&model);
tv.setItemDelegate(new CompleterDelegate(&tv));
tv.show();

return app.exec();
}

ChrisW67
7th July 2011, 10:26
Further tinkering shows that the editor is destroyed after line 7 of the debug output before user control is returned.

A normal editor creation without completer looks like:


DummyDelegate::createEditor() called for QWidget(0x9663a98, name = "qt_scrollarea_viewport") QModelIndex(0,0,0x9665138,QStandardItemModel(0xbfd 09674) )
DummyDelegate::updateEditorGeometry() called for QModelIndex(0,0,0x9665138,QStandardItemModel(0xbfd 09674) )
DummyDelegate::setEditorData() called for QModelIndex(0,0,0x9665138,QStandardItemModel(0xbfd 09674) )