qt - PyQt listview with html rich text delegate moves text bit out of place(pic and code included) -
got listview items need make use of bold text, html delegate way go. looked around , found several solutions , out of them made code, has issue can see in gif, shows results use of delegate , without.
from pyqt4.qtcore import * 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 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_())
i dont use sizehint, since through testing didnt feel affecting behavior. attribute positioning text wrongly, have been able partly counter with:
self.doc.setdocumentmargin(0)
the default margin 4, , move whole item bit right
rect = options.rect options.rect = qrect(rect.x()+3, rect.y(), rect.width(), rect.height())
but dont think addresses cause of issue, masks , becomes problem again once try add icons example
well after more digging , trying, adjusting text rectangle seems give feel allright , seems behave consistently in various des
textrect.adjust(-1, -4, 0, 0)
i not sure if bug, 1 expect se_itemviewitemtext
should have give correct position on own
Comments
Post a Comment