QCompleter::setModel(): Restore completion role

When setting a QFileSystemModel as model, the completion role
is set to QFileSystemModel::FileNameRole. This needs to be reset
to the default Qt::EditRole when setting another model.

Task-number: QTBUG-54642
Change-Id: Ie78d5d417e008ad05a2f995bdbc218b3ad1bc49c
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
bb10
Friedemann Kleint 2016-07-12 16:02:53 +02:00
parent 4397a1b26c
commit 9131f6e561
2 changed files with 21 additions and 2 deletions

View File

@ -1038,6 +1038,10 @@ void QCompleter::setModel(QAbstractItemModel *model)
{
Q_D(QCompleter);
QAbstractItemModel *oldModel = d->proxy->sourceModel();
#ifndef QT_NO_FILESYSTEMMODEL
if (qobject_cast<const QFileSystemModel *>(oldModel))
setCompletionRole(Qt::EditRole); // QTBUG-54642, clear FileNameRole set by QFileSystemModel
#endif
d->proxy->setSourceModel(model);
if (d->popup)
setPopup(d->popup); // set the model and make new connections

View File

@ -291,8 +291,8 @@ retry:
// Testing get/set functions
void tst_QCompleter::getSetCheck()
{
QStandardItemModel model(3,3);
QCompleter completer(&model);
QStandardItemModel standardItemModel(3,3);
QCompleter completer(&standardItemModel);
// QString QCompleter::completionPrefix()
// void QCompleter::setCompletionPrefix(QString)
@ -352,6 +352,21 @@ void tst_QCompleter::getSetCheck()
QCOMPARE(completer.wrapAround(), true); // default value
completer.setWrapAround(false);
QCOMPARE(completer.wrapAround(), false);
#ifndef QT_NO_FILESYSTEMMODEL
// QTBUG-54642, changing from QFileSystemModel to another model should restore role.
completer.setCompletionRole(Qt::EditRole);
QCOMPARE(completer.completionRole(), static_cast<int>(Qt::EditRole)); // default value
QFileSystemModel fileSystemModel;
completer.setModel(&fileSystemModel);
QCOMPARE(completer.completionRole(), static_cast<int>(QFileSystemModel::FileNameRole));
completer.setModel(&standardItemModel);
QCOMPARE(completer.completionRole(), static_cast<int>(Qt::EditRole));
completer.setCompletionRole(Qt::ToolTipRole);
QStandardItemModel standardItemModel2(2, 2); // Do not clobber a custom role when changing models
completer.setModel(&standardItemModel2);
QCOMPARE(completer.completionRole(), static_cast<int>(Qt::ToolTipRole));
#endif // QT_NO_FILESYSTEMMODEL
}
void tst_QCompleter::csMatchingOnCsSortedModel_data()