Results 1 to 5 of 5

Thread: QStyledItemDelegate ignores StyleSheet?

  1. #1
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    3

    Default QStyledItemDelegate ignores StyleSheet?

    I've subclassed a QStyledItemDelegate with the goal to draw a pixmap in the centre of a QTableView’s cell. Our pixmap does get rendered correctly but we lose all style defined in the stylesheet. Removing the delegate restores the style.

    I would like to understand how to use a QStyledItemDelegate while maintaining the existing style. Here’s a PySide delegate that I thought would simply render the cell as is. Applying this delegate removes the style.

    Qt Code:
    1. class MyStyleDelegate(QtGui.QStyledItemDelegate):
    2. def __init__(self, parent=None):
    3. super(MyStyleDelegate, self).__init__(parent)
    4.  
    5. def paint(self, painter, option, index):
    6. super(MyStyleDelegate, self).paint(painter, option, index)
    7.  
    8. modelIndex = index
    9. model = index.model()
    10. if isinstance(model, QtGui.QAbstractProxyModel):
    11. modelIndex = model.mapToSource(index)
    12.  
    13. options = QtGui.QStyleOptionViewItemV4(option)
    14. self.initStyleOption(options, index)
    15. style = options.widget.style() if options.widget else QtGui.QApplication.style()
    16. style.drawControl(style.CE_ItemViewItem, options, painter, options.widget)
    17. QtGui.QStyledItemDelegate.paint(self, painter, option, modelIndex)
    To copy to clipboard, switch view to plain text mode 

    When I step through this code, our widget is always None. That seems to contradict all the examples that I've found.

    We're also differing from the examples and using a QTableView with a proxymodel but from what I've read that should not affect the style.

    Any help would be welcomed.

    (This has been cross-posted from a colleague's post on http://qt-project.org/forums/viewthread/37757/. We have yet to decide which is the best resource for our PySide questions.)

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStyledItemDelegate ignores StyleSheet?

    What style are you expecting to see? An item view cell is typically rendered using style information derived from the model's various roles (in initStyleOption()) for the relevant index.

  3. #3
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    3

    Default Re: QStyledItemDelegate ignores StyleSheet?

    Quote Originally Posted by ChrisW67 View Post
    What style are you expecting to see? An item view cell is typically rendered using style information derived from the model's various roles (in initStyleOption()) for the relevant index.
    I need help understanding what you are saying, in my stylesheet I have:
    Qt Code:
    1. QWidget:item:hover
    2. {
    3. background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffffff, stop: 1 #000000);
    4. color: #000000;
    5. }
    To copy to clipboard, switch view to plain text mode 
    My style is set in my QMainWindow and when I hover over an item in the QTableView without the QStyledItemDelegate enabled, I get the expected black and white gradient. When I enable the QStyledItemDelegate, the hover style does not appear. My expectations is that since my QStyledItemDelegate is not explicitly manipulating the hover case, the hover should remain as a black and white gradient.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QStyledItemDelegate ignores StyleSheet?

    At line six you render the item in its default form.
    The remaining lines render the same thing two more times over the top of that after going out of its way to get a model index for the wrong model.

    In spite of that your code works here when the style sheet typo (line 43) is fixed:
    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui
    3.  
    4. class MyStyleDelegate(QtGui.QStyledItemDelegate):
    5. def __init__(self, parent=None):
    6. super(MyStyleDelegate, self).__init__(parent)
    7.  
    8. def paint(self, painter, option, index):
    9. super(MyStyleDelegate, self).paint(painter, option, index)
    10.  
    11. modelIndex = index
    12. model = index.model()
    13. if isinstance(model, QtGui.QAbstractProxyModel):
    14. modelIndex = model.mapToSource(index)
    15.  
    16. options = QtGui.QStyleOptionViewItemV4(option)
    17. self.initStyleOption(options, index)
    18. style = options.widget.style() if options.widget else QtGui.QApplication.style()
    19. style.drawControl(style.CE_ItemViewItem, options, painter, options.widget)
    20. QtGui.QStyledItemDelegate.paint(self, painter, option, modelIndex)
    21.  
    22. def main():
    23. app = QtGui.QApplication(sys.argv)
    24.  
    25. model = QtGui.QStandardItemModel(5, 5)
    26. for r in range(5):
    27. for c in range(5):
    28. item = QtGui.QStandardItem("R%s C%s" % (r ,c))
    29. model.setItem(r, c, item)
    30.  
    31. proxy = QtGui.QSortFilterProxyModel()
    32. proxy.setSourceModel(model)
    33.  
    34. w = QtGui.QTableView()
    35. w.setModel(proxy)
    36. w.setSortingEnabled(True)
    37. w.show()
    38.  
    39. delegate = MyStyleDelegate()
    40. w.setItemDelegate(delegate)
    41.  
    42. w.setStyleSheet('''
    43. QWidget::item:hover {
    44. background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffffff, stop: 1 #000000);
    45. color: #000000; }
    46. ''')
    47.  
    48.  
    49. sys.exit(app.exec_())
    50.  
    51. if __name__ == '__main__':
    52. main()
    To copy to clipboard, switch view to plain text mode 
    You can remove lines 9 to 15 and 20 with the same result

    I have used PyQt4.

    Edit: PySide 1.1.2 (Linux) does not render the hover gradient at all even with a base QStyledItemDelegate. This is a bug, probably one/both of these:
    https://bugreports.qt-project.org/browse/PYSIDE-152
    https://bugreports.qt-project.org/browse/PYSIDE-115
    Last edited by ChrisW67; 5th February 2014 at 03:38. Reason: updated contents

  5. #5
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    3

    Default Re: QStyledItemDelegate ignores StyleSheet?

    This is unfortunate as I am using PySide.

    My goal of this was to render a pixmap centred in the cell. If the QStyledItemDelegate does not work for me, could you suggest an alternative?

Similar Threads

  1. Replies: 2
    Last Post: 17th July 2011, 14:14
  2. QSplitter ignores some widget methods
    By lotek in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2010, 20:29
  3. sales@trolltech.com ignores me
    By snikolaev in forum General Discussion
    Replies: 1
    Last Post: 27th March 2008, 19:42
  4. Qt4.4 ignores event->ignore()
    By babu198649 in forum Qt Programming
    Replies: 0
    Last Post: 10th January 2008, 10:30
  5. qmake ignores network?
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 24th June 2007, 09:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.