diff options
author | Elias Hautala <Elias.Hautala@qt.io> | 2022-08-03 11:25:49 +0300 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-08-15 13:45:12 +0000 |
commit | 0e2dda5fbe11c18983d3f0cd3d547b9048c9bdbe (patch) | |
tree | ab61c23c743aaccf2fc45a1d3e79dc0704fb8650 /examples/widgets/widgets | |
parent | 790f524ec484ca3fc27fbc9531a0d6817195be20 (diff) |
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 <richard.gustavsen@qt.io>
Reviewed-by: Jani Korteniemi <jani.korteniemi@qt.io>
(cherry picked from commit 1a6019438881b9aa54cfc8dbd3f8fc06ddc35d67)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples/widgets/widgets')
-rw-r--r-- | examples/widgets/widgets/sliders/window.cpp | 41 | ||||
-rw-r--r-- | 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 916f206379..fc6ac6f193 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 <QCheckBox> #include <QComboBox> -#include <QHBoxLayout> #include <QLabel> #include <QSpinBox> #include <QStackedWidget> @@ -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 4894781ac2..7c2297bd83 100644 --- a/examples/widgets/widgets/sliders/window.h +++ b/examples/widgets/widgets/sliders/window.h @@ -52,6 +52,7 @@ #define WINDOW_H #include <QWidget> +#include <QGridLayout> 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] |