Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [PySide] QStyledItemDelegate ignores StyleSheet?
QtWS25 Last Chance

[PySide] QStyledItemDelegate ignores StyleSheet?

Scheduled Pinned Locked Moved Language Bindings
2 Posts 1 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thoimej
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thoimej
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved