QComboBox: fix use in QDataWidgetMapper/QItemDelegate

QItemDelegate and QDataWidgetMapper use the WRITE method on the USER
property to set a value in a widget. This did not work for QComboBox
whose USER property currentText lacked a WRITE method.

This change adds the missing setter and flags it as the WRITE method.

The setter setCurrentText() simply calls setEditText() if the combo
box is editable. Otherwise, if there is a matching text in the list,
currentIndex is set to the corresponding index.

Test included.

Follow-up to 816c554017 which restored
currentText as the USER property.

Task-number: QTBUG-26501
Change-Id: I5f2f999e60b09728ca03ead4e28fe36d1f3ee189
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
Reviewed-by: David Faure <david.faure@kdab.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Mark Brand 2012-10-13 13:41:30 +02:00 committed by The Qt Project
parent 4fbdb969fb
commit 84787d82ee
4 changed files with 74 additions and 4 deletions

View File

@ -2001,6 +2001,17 @@ void QComboBox::setCurrentIndex(int index)
d->setCurrentIndex(mi);
}
void QComboBox::setCurrentText(const QString &text)
{
if (isEditable()) {
setEditText(text);
} else {
const int i = findText(text);
if (i > -1)
setCurrentIndex(i);
}
}
void QComboBoxPrivate::setCurrentIndex(const QModelIndex &mi)
{
Q_Q(QComboBox);
@ -2034,7 +2045,11 @@ void QComboBoxPrivate::setCurrentIndex(const QModelIndex &mi)
by the line edit. Otherwise, it is the value of the current item or
an empty string if the combo box is empty or no current item is set.
\sa editable
The setter setCurrentText() simply calls setEditText() if the combo box is editable.
Otherwise, if there is a matching text in the list, currentIndex is set to the
corresponding index.
\sa editable, setEditText()
*/
QString QComboBox::currentText() const
{

View File

@ -66,7 +66,7 @@ class Q_WIDGETS_EXPORT QComboBox : public QWidget
Q_ENUMS(SizeAdjustPolicy)
Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
Q_PROPERTY(int count READ count)
Q_PROPERTY(QString currentText READ currentText USER true)
Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText USER true)
Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)
Q_PROPERTY(int maxVisibleItems READ maxVisibleItems WRITE setMaxVisibleItems)
Q_PROPERTY(int maxCount READ maxCount WRITE setMaxCount)
@ -211,6 +211,7 @@ public Q_SLOTS:
void clearEditText();
void setEditText(const QString &text);
void setCurrentIndex(int index);
void setCurrentText(const QString &text);
Q_SIGNALS:
void editTextChanged(const QString &);

View File

@ -362,8 +362,10 @@ void tst_QDataWidgetMapper::comboBox()
mapper.addMapping(&readWriteBox, 1, "currentText");
mapper.toFirst();
// setCurrentIndex caused the value at index 0 to be displayed
QCOMPARE(readOnlyBox.currentText(), QString("read only item 0"));
QCOMPARE(readWriteBox.currentText(), QString("read write item 0"));
// setCurrentText set the value in the line edit since the combobox is editable
QCOMPARE(readWriteBox.currentText(), QString("item 0 1"));
// set some new values on the boxes
readOnlyBox.setCurrentIndex(1);
@ -380,7 +382,6 @@ void tst_QDataWidgetMapper::comboBox()
model->setData(model->index(0, 1), QString("read write item z"), Qt::EditRole);
QCOMPARE(readOnlyBox.currentIndex(), 2);
QEXPECT_FAIL("", "See task 125493 and QTBUG-428", Abort);
QCOMPARE(readWriteBox.currentText(), QString("read write item z"));
}

View File

@ -121,6 +121,8 @@ private slots:
void modelDeleted();
void setMaxCount();
void setCurrentIndex();
void setCurrentText_data();
void setCurrentText();
void convenienceViews();
void findText_data();
void findText();
@ -1355,6 +1357,57 @@ void tst_QComboBox::setCurrentIndex()
QCOMPARE(testWidget->currentText(), QString("foo"));
}
void tst_QComboBox::setCurrentText_data()
{
QTest::addColumn<bool>("editable");
QTest::newRow("editable") << true;
QTest::newRow("not editable") << false;
}
void tst_QComboBox::setCurrentText()
{
QFETCH(bool, editable);
QCOMPARE(testWidget->count(), 0);
testWidget->addItems(QStringList() << "foo" << "bar");
QCOMPARE(testWidget->count(), 2);
testWidget->setEditable(editable);
testWidget->setCurrentIndex(0);
QCOMPARE(testWidget->currentIndex(), 0);
// effect on currentText and currentIndex
// currentIndex not changed if editable
QCOMPARE(testWidget->currentText(), QString("foo"));
testWidget->setCurrentText(QString("bar"));
QCOMPARE(testWidget->currentText(), QString("bar"));
if (editable)
QCOMPARE(testWidget->currentIndex(), 0);
else
QCOMPARE(testWidget->currentIndex(), 1);
testWidget->setCurrentText(QString("foo"));
QCOMPARE(testWidget->currentIndex(), 0);
QCOMPARE(testWidget->currentText(), QString("foo"));
// effect of text not found in list
testWidget->setCurrentText(QString("qt"));
QCOMPARE(testWidget->currentIndex(), 0);
if (editable)
QCOMPARE(testWidget->currentText(), QString("qt"));
else
QCOMPARE(testWidget->currentText(), QString("foo"));
#ifndef QT_NO_PROPERTIES
// verify WRITE for currentText property
testWidget->setCurrentIndex(0);
const QByteArray n("currentText");
QCOMPARE(testWidget->property(n).toString(), QString("foo"));
testWidget->setProperty(n, QString("bar"));
QCOMPARE(testWidget->property(n).toString(), QString("bar"));
#endif
}
void tst_QComboBox::editTextChanged()
{
QCOMPARE(testWidget->count(), 0);