From 0e2dda5fbe11c18983d3f0cd3d547b9048c9bdbe Mon Sep 17 00:00:00 2001 From: Elias Hautala Date: Wed, 3 Aug 2022 11:25:49 +0300 Subject: [PATCH] Sliders: Add function that changes widgets position on layout Changes the layout to QGridLayout and adds function to change widgets position on the layout depending on the windows aspect ratio. Before this the widgets wouldn't fit on screen when using the example on Android in portrait. Fixes: QTCREATORBUG-27685 Change-Id: I00009cb6c8c250a8333ac3dfa635f70da4576d5e Reviewed-by: Richard Moe Gustavsen Reviewed-by: Jani Korteniemi (cherry picked from commit 1a6019438881b9aa54cfc8dbd3f8fc06ddc35d67) Reviewed-by: Qt Cherry-pick Bot --- examples/widgets/widgets/sliders/window.cpp | 41 ++++++++++++++++++--- examples/widgets/widgets/sliders/window.h | 4 ++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/examples/widgets/widgets/sliders/window.cpp b/examples/widgets/widgets/sliders/window.cpp index 916f2063799..fc6ac6f1938 100644 --- a/examples/widgets/widgets/sliders/window.cpp +++ b/examples/widgets/widgets/sliders/window.cpp @@ -50,10 +50,8 @@ #include "slidersgroup.h" #include "window.h" - #include #include -#include #include #include #include @@ -81,9 +79,10 @@ Window::Window(QWidget *parent) connect(valueSpinBox, &QSpinBox::valueChanged, horizontalSliders, &SlidersGroup::setValue); - QHBoxLayout *layout = new QHBoxLayout; - layout->addWidget(controlsGroup); - layout->addWidget(stackedWidget); + layout = new QGridLayout; + layout->addWidget(stackedWidget, 0, 1); + layout->addWidget(controlsGroup, 0, 0); + setLayout(layout); minimumSpinBox->setValue(0); @@ -157,5 +156,37 @@ void Window::createControls(const QString &title) controlsLayout->addWidget(invertedKeyBindings, 1, 2); controlsLayout->addWidget(orientationCombo, 3, 0, 1, 3); controlsGroup->setLayout(controlsLayout); + } //! [8] + + +void Window::resizeEvent(QResizeEvent *e) +{ + if (width() == 0 || height() == 0) + return; + + const double aspectRatio = double(width()) / double(height()); + + if ((aspectRatio < 1.0) && (oldAspectRatio > 1.0)) { + layout->removeWidget(controlsGroup); + layout->removeWidget(stackedWidget); + + layout->addWidget(stackedWidget, 1, 0); + layout->addWidget(controlsGroup, 0, 0); + + oldAspectRatio = aspectRatio; + } + else if ((aspectRatio > 1.0) && (oldAspectRatio < 1.0)) { + layout->removeWidget(controlsGroup); + layout->removeWidget(stackedWidget); + + layout->addWidget(stackedWidget, 0, 1); + layout->addWidget(controlsGroup, 0, 0); + + oldAspectRatio = aspectRatio; + } +} + + + diff --git a/examples/widgets/widgets/sliders/window.h b/examples/widgets/widgets/sliders/window.h index 4894781ac2f..7c2297bd83e 100644 --- a/examples/widgets/widgets/sliders/window.h +++ b/examples/widgets/widgets/sliders/window.h @@ -52,6 +52,7 @@ #define WINDOW_H #include +#include QT_BEGIN_NAMESPACE class QCheckBox; @@ -73,6 +74,7 @@ public: private: void createControls(const QString &title); + void resizeEvent(QResizeEvent *e); SlidersGroup *horizontalSliders; SlidersGroup *verticalSliders; @@ -88,6 +90,8 @@ private: QSpinBox *maximumSpinBox; QSpinBox *valueSpinBox; QComboBox *orientationCombo; + QGridLayout *layout; + double oldAspectRatio; }; //! [0]