diff options
author | Mark Brand <mabrand@mabrand.nl> | 2012-10-13 15:08:11 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-10-26 15:24:19 +0200 |
commit | 5bddaf76e027a4688a9f26a6a6b3fa80e0903cb7 (patch) | |
tree | 5842ca0a3e8c8454bc2a293c40afa9ab35c6e615 | |
parent | 84787d82ee9369b2a83c5b0568ee62ab602a5528 (diff) |
QComboBox: new signal currentTextChanged
Adds NOTIFY to currentText property.
Test included.
Change-Id: I3e92b585ad6697891d61537c82f6ab9e8beb1a00
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
-rw-r--r-- | src/widgets/widgets/qcombobox.cpp | 20 | ||||
-rw-r--r-- | src/widgets/widgets/qcombobox.h | 3 | ||||
-rw-r--r-- | tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp | 55 |
3 files changed, 75 insertions, 3 deletions
diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index 27fc3f9015..ef908d62c6 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -822,6 +822,14 @@ QStyleOptionComboBox QComboBoxPrivateContainer::comboStyleOption() const */ /*! + \fn void QComboBox::currentTextChanged(const QString &text) + \since 5.0 + + This signal is sent whenever currentText changes. The new value + is passed as \a text. +*/ + +/*! Constructs a combobox with the given \a parent, using the default model QStandardItemModel. */ @@ -980,9 +988,12 @@ void QComboBoxPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIn } if (currentIndex.row() >= topLeft.row() && currentIndex.row() <= bottomRight.row()) { + const QString text = q->itemText(currentIndex.row()); if (lineEdit) { - lineEdit->setText(q->itemText(currentIndex.row())); + lineEdit->setText(text); updateLineEditGeometry(); + } else { + emit q->currentTextChanged(text); } q->update(); } @@ -1242,7 +1253,11 @@ void QComboBoxPrivate::_q_emitCurrentIndexChanged(const QModelIndex &index) { Q_Q(QComboBox); emit q->currentIndexChanged(index.row()); - emit q->currentIndexChanged(itemText(index)); + const QString text = itemText(index); + emit q->currentIndexChanged(text); + // signal lineEdit.textChanged already connected to signal currentTextChanged, so don't emit double here + if (!lineEdit) + emit q->currentTextChanged(text); #ifndef QT_NO_ACCESSIBILITY QAccessibleEvent event(q, QAccessible::NameChanged); QAccessible::updateAccessibility(&event); @@ -1714,6 +1729,7 @@ void QComboBox::setLineEdit(QLineEdit *edit) connect(d->lineEdit, SIGNAL(returnPressed()), this, SLOT(_q_returnPressed())); connect(d->lineEdit, SIGNAL(editingFinished()), this, SLOT(_q_editingFinished())); connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(editTextChanged(QString))); + connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(currentTextChanged(QString))); d->lineEdit->setFrame(false); d->lineEdit->setContextMenuPolicy(Qt::NoContextMenu); d->lineEdit->setFocusProxy(this); diff --git a/src/widgets/widgets/qcombobox.h b/src/widgets/widgets/qcombobox.h index 1d0e892fd8..bce6c98f2c 100644 --- a/src/widgets/widgets/qcombobox.h +++ b/src/widgets/widgets/qcombobox.h @@ -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 WRITE setCurrentText USER true) + Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText NOTIFY currentTextChanged 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) @@ -221,6 +221,7 @@ Q_SIGNALS: void highlighted(const QString &); void currentIndexChanged(int index); void currentIndexChanged(const QString &); + void currentTextChanged(const QString &); protected: void focusInEvent(QFocusEvent *e); diff --git a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp index df0eb89682..3b9f408e73 100644 --- a/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/widgets/widgets/qcombobox/tst_qcombobox.cpp @@ -116,6 +116,8 @@ private slots: void insertOnCurrentIndex(); void textpixmapdata_data(); void textpixmapdata(); + void currentTextChanged_data(); + void currentTextChanged(); void editTextChanged(); void setModel(); void modelDeleted(); @@ -1408,6 +1410,59 @@ void tst_QComboBox::setCurrentText() #endif } +void tst_QComboBox::currentTextChanged_data() +{ + QTest::addColumn<bool>("editable"); + QTest::newRow("editable") << true; + QTest::newRow("not editable") << false; +} + +void tst_QComboBox::currentTextChanged() +{ + QFETCH(bool, editable); + + QCOMPARE(testWidget->count(), 0); + testWidget->addItems(QStringList() << "foo" << "bar"); + QCOMPARE(testWidget->count(), 2); + + QSignalSpy spy(testWidget, SIGNAL(currentTextChanged(QString))); + + testWidget->setEditable(editable); + + // set text in list + testWidget->setCurrentIndex(0); + QCOMPARE(testWidget->currentIndex(), 0); + spy.clear(); + testWidget->setCurrentText(QString("bar")); + QCOMPARE(spy.count(), 1); + QCOMPARE(qvariant_cast<QString>(spy.at(0).at(0)), QString("bar")); + + // set text not in list + testWidget->setCurrentIndex(0); + QCOMPARE(testWidget->currentIndex(), 0); + spy.clear(); + testWidget->setCurrentText(QString("qt")); + if (editable) { + QCOMPARE(spy.count(), 1); + QCOMPARE(qvariant_cast<QString>(spy.at(0).at(0)), QString("qt")); + } else { + QCOMPARE(spy.count(), 0); + } + + // item changed + testWidget->setCurrentIndex(0); + QCOMPARE(testWidget->currentIndex(), 0); + spy.clear(); + testWidget->setItemText(0, QString("ape")); + QCOMPARE(spy.count(), 1); + QCOMPARE(qvariant_cast<QString>(spy.at(0).at(0)), QString("ape")); + // change it back + spy.clear(); + testWidget->setItemText(0, QString("foo")); + QCOMPARE(spy.count(), 1); + QCOMPARE(qvariant_cast<QString>(spy.at(0).at(0)), QString("foo")); +} + void tst_QComboBox::editTextChanged() { QCOMPARE(testWidget->count(), 0); |