Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix toggling of style for widgets internally setting Qt palette #74

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading