Add widgetAdded signal for QStackedWidget and QStackedLayout

Currently, there is no signal provided when a widget is added
or inserted into QStackedWidget or QStackedLayout.

Emit a signal whenever a widget is added or inserted into
QStackedWidget or QStackedLayout.

[ChangeLog][QtWidgets][QStackedWidget] The widgetAdded() signal
emits whenever a widget is added or inserted into QStackedWidget
or QStackedLayout.

Fixes: QTBUG-122748
Change-Id: I3bf658346a5881665214a8d8f77975aa10e497c0
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Dheerendra Purohit 2024-10-17 12:21:04 +05:30 committed by Volker Hilsheimer
parent ea1409afc1
commit 2ebca8cde1
6 changed files with 71 additions and 0 deletions

View File

@ -113,6 +113,17 @@ QLayoutItem* QStackedLayoutPrivate::replaceAt(int idx, QLayoutItem *newitem)
\sa removeWidget()
*/
/*!
\fn void QStackedLayout::widgetAdded(int index)
\since 6.9
This signal is emitted whenever a widget is added or inserted.
The widget's \a index is passed as parameter.
\sa addWidget(), insertWidget()
*/
/*!
Constructs a QStackedLayout with no parent.
@ -204,6 +215,7 @@ int QStackedLayout::insertWidget(int index, QWidget *widget)
widget->hide();
widget->lower();
}
emit widgetAdded(index);
return index;
}

View File

@ -57,6 +57,7 @@ public:
Q_SIGNALS:
void widgetRemoved(int index);
void currentChanged(int index);
void widgetAdded(int index);
public Q_SLOTS:
void setCurrentIndex(int index);

View File

@ -88,6 +88,18 @@ public:
\sa removeWidget()
*/
/*!
\fn void QStackedWidget::widgetAdded(int index)
\since 6.9
This signal is emitted whenever a widget is added or inserted.
The widget's \a index is passed as parameter.
\sa addWidget(), insertWidget()
*/
/*!
Constructs a QStackedWidget with the given \a parent.
@ -102,6 +114,8 @@ QStackedWidget::QStackedWidget(QWidget *parent)
this, &QStackedWidget::widgetRemoved);
connect(d->layout, &QStackedLayout::currentChanged,
this, &QStackedWidget::currentChanged);
connect(d->layout, &QStackedLayout::widgetAdded,
this, &QStackedWidget::widgetAdded);
}
/*!

View File

@ -41,6 +41,7 @@ public Q_SLOTS:
Q_SIGNALS:
void currentChanged(int);
void widgetRemoved(int index);
void widgetAdded(int index);
protected:
bool event(QEvent *e) override;

View File

@ -31,6 +31,7 @@ private slots:
void keepFocusAfterSetCurrent();
void heigthForWidth();
void replaceWidget();
void widgetAdded();
private:
QWidget *testWidget;
@ -353,6 +354,27 @@ void tst_QStackedLayout::replaceWidget()
QCOMPARE(stackLayout->currentWidget(), replaceTo);
}
void tst_QStackedLayout::widgetAdded()
{
QStackedLayout stackedLayout;
QSignalSpy addSpy(&stackedLayout, &QStackedLayout::widgetAdded);
QVERIFY(addSpy.isValid());
stackedLayout.addWidget(new QWidget);
QCOMPARE(addSpy.count(), 1);
QCOMPARE(addSpy.at(0).at(0).toInt(), 0);
stackedLayout.insertWidget(1, new QWidget);
QCOMPARE(addSpy.count(), 2);
QCOMPARE(addSpy.at(1).at(0).toInt(), 1);
stackedLayout.insertWidget(100, new QWidget);
QCOMPARE(addSpy.count(), 3);
QCOMPARE(addSpy.at(2).at(0).toInt(), 2);
}
QTEST_MAIN(tst_QStackedLayout)
#include "tst_qstackedlayout.moc"

View File

@ -10,6 +10,7 @@
#include <qpushbutton.h>
#include <QHBoxLayout>
#include <qlineedit.h>
#include <QSignalSpy>
#include <QtWidgets/private/qapplication_p.h>
@ -25,6 +26,7 @@ private slots:
void getSetCheck();
void testMinimumSize();
void dynamicPages();
void widgetAdded();
};
tst_QStackedWidget::tst_QStackedWidget()
@ -172,5 +174,24 @@ void tst_QStackedWidget::dynamicPages()
}
void tst_QStackedWidget::widgetAdded()
{
QStackedWidget stackedWidget;
QSignalSpy addSpy(&stackedWidget, &QStackedWidget::widgetAdded);
QVERIFY(addSpy.isValid());
stackedWidget.addWidget(new QWidget);
QCOMPARE(addSpy.count(), 1);
QCOMPARE(addSpy.at(0).at(0).toInt(), 0);
stackedWidget.insertWidget(1, new QWidget);
QCOMPARE(addSpy.count(), 2);
QCOMPARE(addSpy.at(1).at(0).toInt(), 1);
stackedWidget.insertWidget(100, new QWidget);
QCOMPARE(addSpy.count(), 3);
QCOMPARE(addSpy.at(2).at(0).toInt(), 2);
}
QTEST_MAIN(tst_QStackedWidget)
#include "tst_qstackedwidget.moc"