Skip to content

Commit

Permalink
BUG: Fix toggling of style for widgets internally setting Qt palette
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed Aug 21, 2024
1 parent a46147c commit e64f5a4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions {{cookiecutter.project_name}}/Modules/Scripted/Home/Home.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def toolbarNames(self) -> list[str]:

_toolbars: Mapping[str, qt.QToolBar] = {}

_slicerDefaultPalette: Optional[qt.QPalette] = None

def __init__(self, parent: Optional[qt.QWidget]):
"""Called when the application opens the module the first time and the widget is initialized."""
ScriptedLoadableModuleWidget.__init__(self, parent)
Expand All @@ -70,6 +72,8 @@ def setup(self):
# See https://github.com/KitwareMedical/SlicerCustomAppTemplate/issues/72
self.uiWidget.setPalette(slicer.util.mainWindow().style().standardPalette())

self._slicerDefaultPalette = slicer.app.palette()

# Remove unneeded UI elements
self.modifyWindowUI()
self.setCustomUIVisible(True)
Expand Down Expand Up @@ -134,7 +138,10 @@ def toggleStyle(self, visible: bool):
if visible:
self.applyApplicationStyle()
else:
# Modern Qt controls use Qt Stylesheets (.qss) to control dynamic styling
slicer.app.styleSheet = ""
# Legacy Qt-derived controls (see CTK controls) use Qt palettes for styling
slicer.app.setPalette(self._slicerDefaultPalette)

def raiseSettings(self, _):
self.settingsDialog.exec()
Expand All @@ -144,9 +151,20 @@ def setCustomUIVisible(self, visible: bool):

def applyApplicationStyle(self):
SlicerCustomAppUtilities.applyStyle([slicer.app], self.resourcePath("Home.qss"))
self.applyApplicationPalette()
self.styleThreeDWidget()
self.styleSliceWidgets()

def applyApplicationPalette(self):
"""Apply custom palette colors to the application as a workaround for restyling
custom CTK and qMRML controls that do not yet respect Qt stylesheets.
"""
highlightColor = qt.QColor("#009D49") # Kitware - Green

p = self._slicerDefaultPalette
p.setColor(qt.QPalette.Highlight, highlightColor)
slicer.app.setPalette(p)

def styleThreeDWidget(self):
viewNode = slicer.app.layoutManager().threeDWidget(0).mrmlViewNode() # noqa: F841
# viewNode.SetBackgroundColor(0.0, 0.0, 0.0)
Expand Down

0 comments on commit e64f5a4

Please sign in to comment.