qtpyqtenthoughttraitsui

Can't style a modal TraitsUI window with Qt style_sheet


The following view is styled as expected

def traits_view(self):
    style_sheet = '''
        QLabel {
            font: 36pt "Verdana";
            margin-left: 12px;
        }

        QPushButton {
            font: 36pt "Verdana";
            margin: 16px;
            padding: 8px
        }
        '''

    return QtView(
        spring,
        Label(self.msg),
        spring,
        buttons=self.buttons,
        height=1.0,
        width=1.0,
        style_sheet=style_sheet,
        )

But if I make the view modal, the styling goes away:

def traits_view(self):
    style_sheet = '''
        QLabel {
            font: 36pt "Verdana";
            margin-left: 12px;
        }

        QPushButton {
            font: 36pt "Verdana";
            margin: 16px;
            padding: 8px
        }
        '''

    return QtView(
        Label(self.msg),
        buttons=self.buttons,
        kind='modal', # <---- this is the only change
        height=1.0,
        width=1.0,
        style_sheet=style_sheet,
        )

How can I style a modal view?


Solution

  • This is probably a bug, in which the control attribute of the ui isn't available when TraitsUI tries to set the style sheet:

    Traceback (most recent call last):
      File "test3.py", line 61, in <module>
        alert.configure_traits()
      File "/usr/local/lib/python2.7/dist-packages/traitsui/handler.py", line 459, in configure_traits
        scrollable, **args )
      File "/usr/local/lib/python2.7/dist-packages/traits/has_traits.py", line 2156, in configure_traits
        kind, handler, id, scrollable, args )
      File "/usr/local/lib/python2.7/dist-packages/traitsui/qt4/toolkit.py", line 213, in view_application
        id, scrollable, args )
      File "/usr/local/lib/python2.7/dist-packages/traitsui/qt4/view_application.py", line 83, in view_application
        scrollable, args ).ui.result
      File "/usr/local/lib/python2.7/dist-packages/traitsui/qt4/view_application.py", line 134, in __init__
        args       = self.args )
      File "/usr/local/lib/python2.7/dist-packages/traitsui/qt4/extra/qt_view.py", line 57, in ui
        ui.control.setStyleSheet(self.style_sheet)
    AttributeError: 'NoneType' object has no attribute 'setStyleSheet'
    

    For a workaround, use setStyleSheet directly sometime when the control is available.