Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[PySide] QStyledItemDelegate ignores StyleSheet?



  • I am subclassing a QStyledItemDelegate to draw a pixmap in the center of a QTableView's cell but when I implement my delegate I lose all stylesheet customizations and the QTableView is rendered with the default system settings.

    Here's what I do: I create a delgate...
    @
    class MyStyleDelegate(QtGui.QStyledItemDelegate):
    def init(self, parent=None):
    super(MyStyleDelegate, self).init(parent)
    @

    It's a barebones class and I set it to the column delegate of my QTableView
    @
    self.view.setItemDelegateForColumn(0, MyStyleDelegate(self.parent()))
    @

    Now, when I run my application my style sheet is respected - but when I start to override functions, like:
    @
    class MyStyleDelegate(QtGui.QStyledItemDelegate):
    def init(self, parent=None):
    super(MyStyleDelegate, self).init(parent)

    def paint(self, painter, option, index):
        super(MyStyleDelegate, self).paint(painter, option, index) 
    

    @

    (again, just a barebones function calling the base class) I lose all stylesheet customizations.

    Why?

    And how can I implement a StyleDelegate that will respect the style sheet by default?



  • After working with some examples found on the web, my paint function looks like this now. My understanding the below code should render - but respect the stylesheet.
    @
    def paint(self, painter, option, index):
    super(MyStyleDelegate, self).paint(painter, option, index)

        modelIndex = index
        model = index.model()
        if isinstance(model, QtGui.QAbstractProxyModel):
            modelIndex = model.mapToSource(index)
    
        options = QtGui.QStyleOptionViewItemV4(option)
        self.initStyleOption(options, index)
        style = options.widget.style() if options.widget else  QtGui.QApplication.style()  
        style.drawControl(style.CE_ItemViewItem, options, painter, options.widget)
        QtGui.QStyledItemDelegate.paint(self, painter, option, modelIndex) 
    

    @

    Some thing of interest - my options.widget is always None. Would that have the the effect that I'm seeing?


Log in to reply