diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2019-06-20 14:50:48 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2019-12-12 23:24:29 +0100 |
commit | 072ca960f517f0cdfcb768dcbeebba2212a6c21e (patch) | |
tree | 53a55ceb2be475dd5a7b1a0830b7d9b79b79a5a9 /examples/widgets/widgets/styles/widgetgallery.cpp | |
parent | da0e7457523a5c6867c2d9b9f0346167738f0323 (diff) |
styles example: Set default style up front, and react to style changes
Allows running the example with -style foo or QT_STYLE_OVERRIDE.
No changes to documentation needed.
Change-Id: Id7cef450f13faabd118badde51afb7273439c9fc
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'examples/widgets/widgets/styles/widgetgallery.cpp')
-rw-r--r-- | examples/widgets/widgets/styles/widgetgallery.cpp | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/examples/widgets/widgets/styles/widgetgallery.cpp b/examples/widgets/widgets/styles/widgetgallery.cpp index 7e1616c6c0..d70c503f53 100644 --- a/examples/widgets/widgets/styles/widgetgallery.cpp +++ b/examples/widgets/widgets/styles/widgetgallery.cpp @@ -143,6 +143,7 @@ WidgetGallery::WidgetGallery(QWidget *parent) setLayout(mainLayout); setWindowTitle(tr("Styles")); + styleChanged(); } //! [4] @@ -150,12 +151,10 @@ WidgetGallery::WidgetGallery(QWidget *parent) void WidgetGallery::changeStyle(const QString &styleName) //! [5] //! [6] { - if (styleName == "NorwegianWood") { + if (styleName == "NorwegianWood") QApplication::setStyle(new NorwegianWoodStyle); - } else { + else QApplication::setStyle(QStyleFactory::create(styleName)); - } - changePalette(); } //! [6] @@ -170,6 +169,25 @@ void WidgetGallery::changePalette() } //! [8] +void WidgetGallery::changeEvent(QEvent *event) +{ + if (event->type() == QEvent::StyleChange) + styleChanged(); +} + +void WidgetGallery::styleChanged() +{ + auto styleName = QApplication::style()->objectName(); + for (int i = 0; i < styleComboBox->count(); ++i) { + if (QString::compare(styleComboBox->itemText(i), styleName, Qt::CaseInsensitive) == 0) { + styleComboBox->setCurrentIndex(i); + break; + } + } + + changePalette(); +} + //! [9] void WidgetGallery::advanceProgressBar() //! [9] //! [10] |