Skip to content

Commit

Permalink
ENH: Example how to use dynamic properties and stylesheets
Browse files Browse the repository at this point in the history
  • Loading branch information
Punzo committed May 6, 2024
1 parent 7ed1da3 commit b27e8de
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Applications/ctkDICOMVisualBrowser/ctkDICOMVisualBrowserMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
13 changes: 13 additions & 0 deletions Libs/DICOM/Widgets/ctkDICOMVisualBrowserWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QComboBox*>(widget);
if (comboBox)
Expand All @@ -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);
}

//----------------------------------------------------------------------------
Expand Down

0 comments on commit b27e8de

Please sign in to comment.