From b27e8dea731293c4f8a02aef587f93d2f59d7486 Mon Sep 17 00:00:00 2001 From: Davide Punzo Date: Thu, 25 Apr 2024 10:54:32 +0200 Subject: [PATCH] ENH: Example how to use dynamic properties and stylesheets --- .../ctkDICOMVisualBrowserMain.cpp | 45 +++++++++++++++++++ .../Widgets/ctkDICOMVisualBrowserWidget.cpp | 13 ++++++ 2 files changed, 58 insertions(+) diff --git a/Applications/ctkDICOMVisualBrowser/ctkDICOMVisualBrowserMain.cpp b/Applications/ctkDICOMVisualBrowser/ctkDICOMVisualBrowserMain.cpp index 419f10fda2..aaf7a26610 100644 --- a/Applications/ctkDICOMVisualBrowser/ctkDICOMVisualBrowserMain.cpp +++ b/Applications/ctkDICOMVisualBrowser/ctkDICOMVisualBrowserMain.cpp @@ -118,5 +118,50 @@ int main(int argc, char** argv) mainWidget.show(); DICOMVisualBrowser.onShowPatients(); + // CTK should define these styles in files like ctkLightStyle.qss + QString barebonesLightStyleExample = + R""""( + ctkSearchBox[warning=true] + { + background-color: #9c9c27; + } + + ctkCheckableComboBox[warning=true] + { + background-color: #9c9c27; + } + + ctkComboBox[warning=true] + { + background-color: #9c9c27; + } + )""""; + + QString barebonesDarkStyleExample = + R""""( + QWidget + { + background-color: #333333; + color: #ffffff; + } + + ctkSearchBox[warning=true] + { + background-color: #9c9c27; + } + + ctkCheckableComboBox[warning=true] + { + background-color: #9c9c27; + } + + ctkComboBox[warning=true] + { + background-color: #9c9c27; + } + )""""; + + app.setStyleSheet(barebonesLightStyleExample); + return app.exec(); } diff --git a/Libs/DICOM/Widgets/ctkDICOMVisualBrowserWidget.cpp b/Libs/DICOM/Widgets/ctkDICOMVisualBrowserWidget.cpp index 3022e0e461..204302095c 100644 --- a/Libs/DICOM/Widgets/ctkDICOMVisualBrowserWidget.cpp +++ b/Libs/DICOM/Widgets/ctkDICOMVisualBrowserWidget.cpp @@ -889,6 +889,7 @@ void ctkDICOMVisualBrowserWidgetPrivate::setBackgroundColorToWidget(QColor color return; } + // This will be applied if no styleSheet has been set on the widget QPalette pal = widget->palette(); QComboBox* comboBox = qobject_cast(widget); if (comboBox) @@ -900,6 +901,18 @@ void ctkDICOMVisualBrowserWidgetPrivate::setBackgroundColorToWidget(QColor color pal.setColor(widget->backgroundRole(), color); } widget->setPalette(pal); + + // If the widget has a styleSheet, the styleSheet will overrride the palette + if (ctkDICOMVisualBrowserWidgetWarningColor == color) + { + widget->setProperty("warning", true); + } + else + { + widget->setProperty("warning", false); + } + widget->style()->unpolish(widget); + widget->style()->polish(widget); } //----------------------------------------------------------------------------