summaryrefslogtreecommitdiffstats
path: root/src/widgets/kernel/qlayout.cpp
diff options
context:
space:
mode:
authorThorbjørn Lund Martsum <tmartsum@gmail.com>2012-09-03 06:35:39 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-09-21 23:17:55 +0200
commit42d681f9cfc984046a93b9efe19903d46ac68bb1 (patch)
tree0a93a177458dc661b9ceb200f8fa21250b6b601f /src/widgets/kernel/qlayout.cpp
parente327ba191df56f3315a004d7650d5f1080f12acb (diff)
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 <jpnurmi@digia.com>
Diffstat (limited to 'src/widgets/kernel/qlayout.cpp')
-rw-r--r--src/widgets/kernel/qlayout.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/widgets/kernel/qlayout.cpp b/src/widgets/kernel/qlayout.cpp
index 8541dd984d..b79a6fe6c7 100644
--- a/src/widgets/kernel/qlayout.cpp
+++ b/src/widgets/kernel/qlayout.cpp
@@ -1109,6 +1109,54 @@ bool QLayout::activate()
}
/*!
+ \since 5.2
+
+ Searches for widget \a from and replaces it with widget \a to if found.
+ Returns the layout item that contains the widget \a from on success. Otherwise \c 0 is returned.
+ If \a recursive is \c true, sub-layouts are searched for doing the replacement. Notice that the returned item therefore might not belong to this layout, but to a sub-layout.
+
+ The returned layout item is no longer owned by the layout and should be either deleted or inserted to another layout. The widget \a from is no longer managed by the layout and may need to be deleted or hidden. The parent of widget \a from is left unchanged.
+
+ This function works for the built-in Qt layouts, but might not work for custom layouts.
+
+ \sa indexOf()
+*/
+
+//### Qt 6 make this function virtual
+QLayoutItem* QLayout::replaceWidget(QWidget *from, QWidget *to, bool recursive)
+{
+ Q_D(QLayout);
+ if (!from || !to)
+ return 0;
+
+ int index = -1;
+ QLayoutItem *item = 0;
+ for (int u = 0; u < count(); ++u) {
+ item = itemAt(u);
+ if (item->widget() == from) {
+ index = u;
+ break;
+ }
+ if (item && item->layout() && recursive) {
+ QLayoutItem *r = item->layout()->replaceWidget(from, to, true);
+ if (r)
+ return r;
+ }
+ }
+ if (index == -1)
+ return 0;
+
+ QLayoutItem *newitem = new QWidgetItem(to);
+ newitem->setAlignment(item->alignment());
+ QLayoutItem *r = d->replaceAt(index, newitem);
+ if (!r)
+ delete newitem;
+ else
+ addChildWidget(to);
+ return r;
+}
+
+/*!
\fn QLayoutItem *QLayout::itemAt(int index) const
Must be implemented in subclasses to return the layout item at \a