Results 1 to 1 of 1

Thread: Use of QStyledItemDelegate prevents certain Stylesheets from working [gif]

  1. #1
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Question Use of QStyledItemDelegate prevents certain Stylesheets from working [gif]

    Here is a gif showing the problem, when I dont use delegate we can see red background and magenta highlight but bold tags are ignored.
    So I enable my QStyledItemDelegate to get bold text working and we see that the colors from stylesheet get ignored.



    Here is the simple code from the example where we can see the delegate used. Is there a way to fix delegate? Me quickly trying stuff, seems that rules that use double colon gets ignored?

    Interesting thing is that I only now noticed some stylesheets getting ignored. I used popular dark theme stylesheet and it seemed that its working for most stuff. The problem appears when I need to control color of the selection highlight in tableview, to make it blink red when something is wrong with the item and user double clicks it, I had related question here about it, Main problem then was that use of self.setStyleSheet('selection-background-color:red;') did not work on ubuntu like distros, while it did on KDE, i3. Now use of QTableView::item:selected:active {...} works everywhere flawlessly unless I use the rich html text delegate that I am using... which might be badly written, I am far from 100% in writing up delegates.

    Qt Code:
    1. from PyQt5.QtCore import *
    2. from PyQt5.QtGui import *
    3. from PyQt5.QtWidgets import *
    4. import sys
    5.  
    6.  
    7. class My_Model_table(QAbstractTableModel):
    8. def __init__(self, table_data=[], parent=None):
    9. super().__init__()
    10. self.table_data = table_data
    11.  
    12. def rowCount(self, parent):
    13. return len(self.table_data)
    14.  
    15. def columnCount(self, parent):
    16. return 1
    17.  
    18. def data(self, index, role):
    19. if role == Qt.DisplayRole:
    20. value = self.table_data[index.row()]
    21. return value
    22. if role == Qt.TextAlignmentRole:
    23. return Qt.AlignCenter
    24.  
    25.  
    26. class My_table(QTableView):
    27. def __init__(self, parent=None):
    28. super().__init__()
    29.  
    30. self.setItemDelegate(self.HTMLDelegate())
    31.  
    32. self.setStyleSheet(
    33. '''
    34. QTableView::item:selected:active {
    35. background: magenta;
    36. }
    37.  
    38. QTableView::item {
    39. background: red;
    40. }
    41. '''
    42. )
    43.  
    44. class HTMLDelegate(QStyledItemDelegate):
    45. def __init__(self, parent=None):
    46. super().__init__()
    47. self.doc = QTextDocument(self)
    48.  
    49. def paint(self, painter, option, index):
    50. painter.save()
    51.  
    52. options = QStyleOptionViewItem(option)
    53.  
    54. self.initStyleOption(options, index)
    55. self.doc.setHtml(options.text)
    56. options.text = ""
    57.  
    58. style = QApplication.style() if options.widget is None \
    59. else options.widget.style()
    60. style.drawControl(QStyle.CE_ItemViewItem, options, painter)
    61.  
    62. ctx = QAbstractTextDocumentLayout.PaintContext()
    63.  
    64. if option.state & QStyle.State_Selected:
    65. ctx.palette.setColor(QPalette.Text, option.palette.color(
    66. QPalette.Active, QPalette.HighlightedText))
    67. else:
    68. ctx.palette.setColor(QPalette.Text, option.palette.color(
    69. QPalette.Active, QPalette.Text))
    70.  
    71. textRect = style.subElementRect(
    72. QStyle.SE_ItemViewItemText, options)
    73.  
    74. textRect.adjust(20, 0, 0, 0)
    75.  
    76. thefuckyourshitup_constant = 4
    77. margin = (option.rect.height() - options.fontMetrics.height()) // 2
    78. margin = margin - thefuckyourshitup_constant
    79. textRect.setTop(textRect.top() + margin)
    80.  
    81. painter.translate(textRect.topLeft())
    82. painter.setClipRect(textRect.translated(-textRect.topLeft()))
    83. self.doc.documentLayout().draw(painter, ctx)
    84.  
    85. painter.restore()
    86.  
    87. def sizeHint(self, option, index):
    88. return QSize(self.doc.idealWidth(), self.doc.size().height())
    89.  
    90.  
    91. if __name__ == '__main__':
    92. app = QApplication(sys.argv)
    93. data = ['normal', 'text', '<b>bold</b> text', 'hmmm', 'yeap <b>bold</b>']
    94. main_table = My_table()
    95. main_table.setModel(My_Model_table(data))
    96. main_table.show()
    97. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by DoTheEvo; 16th February 2016 at 20:22.

Similar Threads

  1. QStyledItemDelegate not working cross platform
    By Berryblue031 in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2013, 12:05
  2. QStyledItemDelegate - style sheet not working
    By tomixer in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2010, 11:12
  3. Working with StyleSheets
    By BingoMaster in forum Newbie
    Replies: 3
    Last Post: 29th September 2009, 17:52
  4. QFileDialog prevents app from closing
    By hersheyzombie in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2009, 11:16
  5. QLabel in StyleSheets isn't working?
    By VireX in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2007, 10:59

Tags for this Thread

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.