From 072ca960f517f0cdfcb768dcbeebba2212a6c21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 20 Jun 2019 14:50:48 +0200 Subject: 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 --- examples/widgets/widgets/styles/widgetgallery.cpp | 26 +++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'examples/widgets/widgets/styles/widgetgallery.cpp') 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] -- cgit v1.2.3