Fix deprecation of QComboBox:::currentIndexChanged(const QString&)

Don't introduce another overload with two parameters. Users
want a simple signal to connect to, not another overload. Deprecate
the currentIndexChanged(QString) overload, usage of that can/should
get replaced by currentTextChanged().

This partially reverts commit 11dc7b35c8.

Change-Id: I5e7d16413f3d62b1a5a7a197f510af2c45cdfa55
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
This commit is contained in:
Lars Knoll 2020-03-25 11:20:28 +01:00
parent b15ed929f3
commit 46ebd11e66
3 changed files with 29 additions and 35 deletions

View File

@ -904,7 +904,8 @@ QStyleOptionComboBox QComboBoxPrivateContainer::comboStyleOption() const
This signal is sent when the user chooses an item in the combobox.
The item's \a index is passed. Note that this signal is sent even
when the choice is not changed. If you need to know when the
choice actually changes, use signal currentIndexChanged().
choice actually changes, use signal currentIndexChanged() or
currentTextChanged().
*/
@ -914,7 +915,8 @@ QStyleOptionComboBox QComboBoxPrivateContainer::comboStyleOption() const
This signal is sent when the user chooses an item in the combobox.
The item's \a text is passed. Note that this signal is sent even
when the choice is not changed. If you need to know when the
choice actually changes, use signal currentIndexChanged().
choice actually changes, use signal currentIndexChanged() or
currentTextChanged().
\obsolete Use QComboBox::textActivated() instead
*/
@ -925,7 +927,8 @@ QStyleOptionComboBox QComboBoxPrivateContainer::comboStyleOption() const
This signal is sent when the user chooses an item in the combobox.
The item's \a text is passed. Note that this signal is sent even
when the choice is not changed. If you need to know when the
choice actually changes, use signal currentIndexChanged().
choice actually changes, use signal currentIndexChanged() or
currentTextChanged().
*/
/*!
@ -959,8 +962,6 @@ QStyleOptionComboBox QComboBoxPrivateContainer::comboStyleOption() const
changes either through user interaction or programmatically. The
item's \a index is passed or -1 if the combobox becomes empty or the
currentIndex was reset.
\obsolete Use currentIndexChanged(int index, const QString &text) instead
*/
/*!
@ -971,17 +972,8 @@ QStyleOptionComboBox QComboBoxPrivateContainer::comboStyleOption() const
changes either through user interaction or programmatically. The
item's \a text is passed.
\obsolete Use currentIndexChanged(int index, const QString &text) instead
*/
/*!
\fn void QComboBox::currentIndexChanged(int index, const QString &text)
\since 5.15
This signal is sent whenever the currentIndex in the combobox
changes either through user interaction or programmatically. The
item's \a index is passed or -1 if the combobox becomes empty or
the currentIndex was reset. The item's \a text is also passed.
\obsolete Use currentIndexChanged(int) and get the text from
the itemText(int) method.
*/
/*!
@ -1013,7 +1005,6 @@ QComboBox::QComboBox(QComboBoxPrivate &dd, QWidget *parent)
d->init();
}
/*!
\class QComboBox
\brief The QComboBox widget is a combined button and popup list.
@ -1036,9 +1027,10 @@ QComboBox::QComboBox(QComboBoxPrivate &dd, QWidget *parent)
to clear the displayed string without changing the combobox's
contents.
There are two signals emitted if the current item of a combobox
changes, currentIndexChanged() and activated().
currentIndexChanged() is always emitted regardless if the change
There are three signals emitted if the current item of a combobox
changes, currentIndexChanged(), currentTextChanged() and activated().
currentIndexChanged() and currentTextChanged() are always emitted
regardless if the change
was done programmatically or by user interaction, while
activated() is only emitted when the change is caused by user
interaction. The highlighted() signal is emitted when the user
@ -1466,14 +1458,13 @@ void QComboBoxPrivate::_q_emitCurrentIndexChanged(const QModelIndex &index)
{
Q_Q(QComboBox);
const QString text = itemText(index);
#if QT_DEPRECATED_SINCE(5, 15)
emit q->currentIndexChanged(index.row());
#if QT_DEPRECATED_SINCE(5, 13)
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
emit q->currentIndexChanged(index.row());
emit q->currentIndexChanged(text);
QT_WARNING_POP
#endif
emit q->currentIndexChanged(index.row(), text);
// signal lineEdit.textChanged already connected to signal currentTextChanged, so don't emit double here
if (!lineEdit)
emit q->currentTextChanged(text);

View File

@ -233,13 +233,12 @@ Q_SIGNALS:
void textActivated(const QString &);
void highlighted(int index);
void textHighlighted(const QString &);
#if QT_DEPRECATED_SINCE(5, 15)
QT_DEPRECATED_VERSION_X_5_15("Use currentIndexChanged(int, const QString &) instead")
void currentIndexChanged(int index);
QT_DEPRECATED_VERSION_X_5_15("Use currentIndexChanged(int, const QString &) instead")
#if QT_DEPRECATED_SINCE(5, 15)
QT_DEPRECATED_VERSION_X_5_15(
"Use currentIndexChanged(int) instead, and get the text using itemText(index)")
void currentIndexChanged(const QString &);
#endif
void currentIndexChanged(int index, const QString &text);
void currentTextChanged(const QString &);
#if QT_DEPRECATED_SINCE(5, 15)
QT_DEPRECATED_VERSION_X(5, 15, "Use textActivated() instead")

View File

@ -1208,7 +1208,8 @@ void tst_QComboBox::currentIndex()
QVERIFY(testWidget->currentText().isEmpty());
// spy on currentIndexChanged
QSignalSpy indexChangedSpy(testWidget, SIGNAL(currentIndexChanged(int, QString)));
QSignalSpy indexChangedInt(testWidget, SIGNAL(currentIndexChanged(int)));
QSignalSpy indexChangedString(testWidget, SIGNAL(currentIndexChanged(QString)));
// stuff items into it
foreach(QString text, initialItems) {
@ -1232,12 +1233,16 @@ void tst_QComboBox::currentIndex()
QCOMPARE(testWidget->currentText(), expectedCurrentText);
// check that signal count is correct
QCOMPARE(indexChangedSpy.count(), expectedSignalCount);
QCOMPARE(indexChangedInt.count(), expectedSignalCount);
QCOMPARE(indexChangedString.count(), expectedSignalCount);
// compare with last sent signal values
if (indexChangedSpy.count())
QCOMPARE(indexChangedSpy.at(indexChangedSpy.count() - 1).at(0).toInt(),
testWidget->currentIndex());
if (indexChangedInt.count())
QCOMPARE(indexChangedInt.at(indexChangedInt.count() - 1).at(0).toInt(),
testWidget->currentIndex());
if (indexChangedString.count())
QCOMPARE(indexChangedString.at(indexChangedString.count() - 1).at(0).toString(),
testWidget->currentText());
if (edit) {
testWidget->setCurrentIndex(-1);
@ -2336,8 +2341,7 @@ public:
{
QStringList list;
list << "one" << "two";
connect(this, SIGNAL(currentIndexChanged(int, QString)),
this, SLOT(onCurrentIndexChanged(int)));
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(onCurrentIndexChanged(int)));
addItems(list);
}
public slots:
@ -2763,7 +2767,7 @@ void tst_QComboBox::resetModel()
};
QComboBox cb;
StringListModel model({"1", "2"});
QSignalSpy spy(&cb, QOverload<int, const QString &>::of(&QComboBox::currentIndexChanged));
QSignalSpy spy(&cb, QOverload<int>::of(&QComboBox::currentIndexChanged));
QCOMPARE(spy.count(), 0);
QCOMPARE(cb.currentIndex(), -1); //no selection