diff options
author | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2023-10-12 10:05:49 +0200 |
---|---|---|
committer | Marcus Tillmanns <marcus.tillmanns@qt.io> | 2023-10-12 10:32:51 +0000 |
commit | db8d82e51e03a8e8675b7f9c29dada636c478078 (patch) | |
tree | f3d23814170e52f5b9316a41be96082fae42511b | |
parent | 644e88ae2eef1e3042ef27e26a3f60141055ce85 (diff) |
Utils: Change Stack to QStackedWidget
We use a QStackedWidget instead of a QStackedLayout here because the
latter will call "setVisible()" when a child is added, which can lead to the
widget being spawned as a top-level widget. This can lead to the focus
shifting away from the main application.
Change-Id: I4746de2ea1a300d7c97735f0bbd9338cd2818b5d
Reviewed-by: hjk <hjk@qt.io>
-rw-r--r-- | src/libs/utils/layoutbuilder.cpp | 35 | ||||
-rw-r--r-- | src/plugins/compilerexplorer/compilerexploreraspects.cpp | 4 |
2 files changed, 26 insertions, 13 deletions
diff --git a/src/libs/utils/layoutbuilder.cpp b/src/libs/utils/layoutbuilder.cpp index 7fa3d5adf33..23def250e25 100644 --- a/src/libs/utils/layoutbuilder.cpp +++ b/src/libs/utils/layoutbuilder.cpp @@ -14,6 +14,7 @@ #include <QSpinBox> #include <QSplitter> #include <QStackedLayout> +#include <QStackedWidget> #include <QStyle> #include <QTabWidget> #include <QTextEdit> @@ -396,14 +397,18 @@ void Slice::flush() for (const ResultItem &item : std::as_const(pendingItems)) addItemToFlowLayout(flowLayout, item); - } else if (auto stackLayout = qobject_cast<QStackedLayout *>(layout)) { + } else if (auto stackWidget = qobject_cast<QStackedWidget *>(widget)) { for (const ResultItem &item : std::as_const(pendingItems)) { if (item.widget) - stackLayout->addWidget(item.widget); - else + stackWidget->addWidget(item.widget); + else if (item.layout) { + auto w = new QWidget(); + w->setLayout(item.layout); + stackWidget->addWidget(w); + } else { QTC_CHECK(false); + } } - } else { QTC_CHECK(false); } @@ -656,13 +661,6 @@ Form::Form(std::initializer_list<LayoutItem> items) onExit = layoutExit; } -Stack::Stack(std::initializer_list<LayoutItem> items) -{ - subItems = items; - onAdd = [](LayoutBuilder &builder) { builder.stack.append(new QStackedLayout); }; - onExit = layoutExit; -} - LayoutItem br() { LayoutItem item; @@ -754,6 +752,21 @@ Group::Group(std::initializer_list<LayoutItem> items) setupWidget<QGroupBox>(this); } +Stack::Stack(std::initializer_list<LayoutItem> items) +{ + // We use a QStackedWidget instead of a QStackedLayout here because the latter will call + // "setVisible()" when a child is added, which can lead to the widget being spawned as a + // top-level widget. This can lead to the focus shifting away from the main application. + subItems = items; + onAdd = [](LayoutBuilder &builder) { builder.stack.append(new QStackedWidget); }; + onExit = [](LayoutBuilder &builder) { + QWidget *widget = builder.stack.last().widget; + builder.stack.last().flush(); + builder.stack.pop_back(); + builder.stack.last().pendingItems.append(ResultItem(widget)); + }; +} + PushButton::PushButton(std::initializer_list<LayoutItem> items) { this->subItems = items; diff --git a/src/plugins/compilerexplorer/compilerexploreraspects.cpp b/src/plugins/compilerexplorer/compilerexploreraspects.cpp index 021914310de..0fd56d10500 100644 --- a/src/plugins/compilerexplorer/compilerexploreraspects.cpp +++ b/src/plugins/compilerexplorer/compilerexploreraspects.cpp @@ -13,7 +13,7 @@ #include <QCompleter> #include <QFutureWatcher> #include <QPushButton> -#include <QStackedLayout> +#include <QStackedWidget> using namespace Utils; @@ -207,7 +207,7 @@ void LibrarySelectionAspect::addToLayout(Layouting::LayoutItem &parent) QPushButton *editBtn = new QPushButton(Tr::tr("Edit")); - QStackedLayout *stack{nullptr}; + QStackedWidget *stack{nullptr}; // clang-format off auto s = Stack { |