From 42d681f9cfc984046a93b9efe19903d46ac68bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lund=20Martsum?= Date: Mon, 3 Sep 2012 06:35:39 +0200 Subject: Add widget replace function to QLayout Sometimes it is nice to be able to replace a widget in a layout. Change-Id: I23a6a65e417e94d53bc48639503db1a142bc3f10 Reviewed-by: J-P Nurmi --- src/widgets/kernel/qformlayout.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/widgets/kernel/qformlayout.cpp') diff --git a/src/widgets/kernel/qformlayout.cpp b/src/widgets/kernel/qformlayout.cpp index fd6482d5af..e1376c754f 100644 --- a/src/widgets/kernel/qformlayout.cpp +++ b/src/widgets/kernel/qformlayout.cpp @@ -246,6 +246,7 @@ public: int hSpacing; int vSpacing; + QLayoutItem* replaceAt(int index, QLayoutItem*) Q_DECL_OVERRIDE; }; QFormLayoutPrivate::QFormLayoutPrivate() @@ -1001,6 +1002,32 @@ QStyle* QFormLayoutPrivate::getStyle() const return QApplication::style(); } +QLayoutItem* QFormLayoutPrivate::replaceAt(int index, QLayoutItem *newitem) +{ + Q_Q(QFormLayout); + if (!newitem) + return 0; + const int storageIndex = storageIndexFromLayoutItem(m_matrix, m_things.value(index)); + if (storageIndex == -1) { + // ### Qt6 - fix warning too when this class becomes public + qWarning("QFormLayoutPrivate::replaceAt: Invalid index %d", index); + return 0; + } + + int row, col; + QFormLayoutPrivate::ItemMatrix::storageIndexToPosition(storageIndex, &row, &col); + Q_ASSERT(m_matrix(row, col)); + + QFormLayoutItem *item = m_matrix(row, col); + Q_ASSERT(item); + + QLayoutItem *olditem = item->item; + item->item = newitem; + + q->invalidate(); + return olditem; +} + /*! \class QFormLayout \since 4.4 -- cgit v1.2.3