ComboBox: fix usage in an asynchronous Loader

The following comment in QQmlDelegateModel::object() helped to find
a solution ie. using createdItem() instead of initItem():

  If asynchronous is true or the component is being loaded asynchronously due
  to an ancestor being loaded asynchronously, item() may return 0.  In this
  case createdItem() will be emitted when the item is available. [...]

Change-Id: If3bf8e60834534ca07c8db8f502f4f11969057e8
Task-number: QTBUG-51972
Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
This commit is contained in:
J-P Nurmi 2016-03-23 10:44:08 +01:00
parent 18624d2d46
commit c59c43bbc4
2 changed files with 29 additions and 4 deletions

View File

@ -165,7 +165,7 @@ public:
void itemClicked();
void initItem(int index, QObject *object);
void createdItem(int index, QObject *object);
void countChanged();
void updateCurrentText();
void increase();
@ -235,7 +235,7 @@ void QQuickComboBoxPrivate::itemClicked()
}
}
void QQuickComboBoxPrivate::initItem(int index, QObject *object)
void QQuickComboBoxPrivate::createdItem(int index, QObject *object)
{
QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(object);
if (button)
@ -316,7 +316,7 @@ void QQuickComboBoxPrivate::createDelegateModel()
if (oldModel) {
disconnect(delegateModel, &QQmlInstanceModel::countChanged, this, &QQuickComboBoxPrivate::countChanged);
disconnect(delegateModel, &QQmlInstanceModel::modelUpdated, this, &QQuickComboBoxPrivate::updateCurrentText);
disconnect(delegateModel, &QQmlInstanceModel::initItem, this, &QQuickComboBoxPrivate::initItem);
disconnect(delegateModel, &QQmlInstanceModel::createdItem, this, &QQuickComboBoxPrivate::createdItem);
}
ownModel = false;
@ -336,7 +336,7 @@ void QQuickComboBoxPrivate::createDelegateModel()
if (delegateModel) {
connect(delegateModel, &QQmlInstanceModel::countChanged, this, &QQuickComboBoxPrivate::countChanged);
connect(delegateModel, &QQmlInstanceModel::modelUpdated, this, &QQuickComboBoxPrivate::updateCurrentText);
connect(delegateModel, &QQmlInstanceModel::initItem, this, &QQuickComboBoxPrivate::initItem);
connect(delegateModel, &QQmlInstanceModel::createdItem, this, &QQuickComboBoxPrivate::createdItem);
}
emit q->delegateModelChanged();

View File

@ -756,4 +756,29 @@ TestCase {
control.destroy()
}
Component {
id: asyncLoader
Loader {
active: false
asynchronous: true
sourceComponent: ComboBox {
model: ["First", "Second", "Third"]
}
}
}
// QTBUG-51972
function test_async() {
var loader = asyncLoader.createObject(testCase)
verify(loader)
loader.active = true
tryCompare(loader, "status", Loader.Ready)
verify(loader.item)
compare(loader.item.currentText, "First")
compare(loader.item.displayText, "First")
loader.destroy()
}
}