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>
bb10
Tor Arne Vestbø 2019-06-20 14:50:48 +02:00
parent da0e745752
commit 072ca960f5
4 changed files with 30 additions and 4 deletions

View File

@ -50,12 +50,15 @@
#include <QApplication>
#include "norwegianwoodstyle.h"
#include "widgetgallery.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(styles);
QApplication::setStyle(new NorwegianWoodStyle);
QApplication app(argc, argv);
WidgetGallery gallery;
gallery.show();

View File

@ -58,6 +58,7 @@
NorwegianWoodStyle::NorwegianWoodStyle() :
QProxyStyle(QStyleFactory::create("windows"))
{
setObjectName("NorwegianWood");
}
//! [0]

View File

@ -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]

View File

@ -80,8 +80,12 @@ class WidgetGallery : public QDialog
public:
WidgetGallery(QWidget *parent = nullptr);
protected:
void changeEvent(QEvent *) override;
private slots:
void changeStyle(const QString &styleName);
void styleChanged();
void changePalette();
void advanceProgressBar();