DoTheEvo
20th May 2015, 23:43
http://i.imgur.com/7BchqwR.gif
I first asked on stackoverflow - link (http://stackoverflow.com/questions/30175644/pyqt-listview-with-html-rich-text-delegate-moves-text-bit-out-of-placepic-and-c)
But no responses, few solutions I found out seems to be band aids that get ripped when I try to do more stuff with it.
I add icons and suddenly I have issues across various desktop environments. And moving text/items up and down by pixes makes it out of place in different DE...
All the while everything is nice, pretty and perfect when custom delegate for html tags is not in use.
heres the test code seen in the gif, theres bit more story on stackoverflow, with also pyqt5 and pyside versions.
Comment out the 39th line to use the default delegate.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class HTMLDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(HTMLDelegate, self).__init__(parent)
self.doc = QTextDocument(self)
def paint(self, painter, option, index):
painter.save()
options = QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
self.doc.setHtml(options.text)
options.text = ""
style = QApplication.style() if options.widget is None \
else options.widget.style()
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.HighlightedText))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
painter.translate(textRect.topLeft())
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
if __name__ == '__main__':
app = QApplication(sys.argv)
data = ['1','2','3','4','5','6','7','8','9']
main_list = QListView()
main_list.setItemDelegate(HTMLDelegate())
main_list.setModel(QStringListModel(data))
main_list.show()
sys.exit(app.exec_())
One thing that might help me get to the point of this, is if I could find the code of the default delegate?
See how it is done, try some stuff... assuming it is somewhere declared in the similar format as how we are suppose to do the custom ones... but as a noobie I am having a hard time finding it
I first asked on stackoverflow - link (http://stackoverflow.com/questions/30175644/pyqt-listview-with-html-rich-text-delegate-moves-text-bit-out-of-placepic-and-c)
But no responses, few solutions I found out seems to be band aids that get ripped when I try to do more stuff with it.
I add icons and suddenly I have issues across various desktop environments. And moving text/items up and down by pixes makes it out of place in different DE...
All the while everything is nice, pretty and perfect when custom delegate for html tags is not in use.
heres the test code seen in the gif, theres bit more story on stackoverflow, with also pyqt5 and pyside versions.
Comment out the 39th line to use the default delegate.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class HTMLDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(HTMLDelegate, self).__init__(parent)
self.doc = QTextDocument(self)
def paint(self, painter, option, index):
painter.save()
options = QStyleOptionViewItemV4(option)
self.initStyleOption(options, index)
self.doc.setHtml(options.text)
options.text = ""
style = QApplication.style() if options.widget is None \
else options.widget.style()
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.HighlightedText))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
painter.translate(textRect.topLeft())
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
if __name__ == '__main__':
app = QApplication(sys.argv)
data = ['1','2','3','4','5','6','7','8','9']
main_list = QListView()
main_list.setItemDelegate(HTMLDelegate())
main_list.setModel(QStringListModel(data))
main_list.show()
sys.exit(app.exec_())
One thing that might help me get to the point of this, is if I could find the code of the default delegate?
See how it is done, try some stuff... assuming it is somewhere declared in the similar format as how we are suppose to do the custom ones... but as a noobie I am having a hard time finding it