Skip to content

Instantly share code, notes, and snippets.

@eyllanesc
Created December 19, 2017 17:45
Show Gist options
  • Save eyllanesc/c524a51e734de0377b2746715b1f721c to your computer and use it in GitHub Desktop.
Save eyllanesc/c524a51e734de0377b2746715b1f721c to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Form.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(613, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.listWidget = QtWidgets.QListWidget(Form)
self.listWidget.setObjectName("listWidget")
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
item = QtWidgets.QListWidgetItem()
self.listWidget.addItem(item)
self.verticalLayout.addWidget(self.listWidget)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
__sortingEnabled = self.listWidget.isSortingEnabled()
self.listWidget.setSortingEnabled(False)
item = self.listWidget.item(0)
item.setText(_translate("Form", "&theta; = &phi;<sup>2</sup> (toto et al.)"))
item = self.listWidget.item(1)
item.setText(_translate("Form", "&theta; = &phi;<sup>2.5</sup> (tata et al.)"))
item = self.listWidget.item(2)
item.setText(_translate("Form", "&theta; = 1-log(&phi;/2) (mister brown et al.)"))
self.listWidget.setSortingEnabled(__sortingEnabled)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>613</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listWidget">
<item>
<property name="text">
<string>&amp;theta; = &amp;phi;&lt;sup&gt;2&lt;/sup&gt; (toto et al.)</string>
</property>
</item>
<item>
<property name="text">
<string>&amp;theta; = &amp;phi;&lt;sup&gt;2.5&lt;/sup&gt; (tata et al.)</string>
</property>
</item>
<item>
<property name="text">
<string>&amp;theta; = 1-log(&amp;phi;/2) (mister brown et al.)</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
from PyQt5 import QtCore, QtGui, QtWidgets
from Form import Ui_Form
class HTMLDelegate(QtWidgets.QStyledItemDelegate):
def paint(self, painter, option, index):
self.initStyleOption(option,index)
painter.save()
doc = QtGui.QTextDocument()
doc.setHtml(option.text)
option.text = ""
option.widget.style().drawControl(QtWidgets.QStyle.CE_ItemViewItem, option, painter)
painter.translate(option.rect.left(), option.rect.top())
clip = QtCore.QRectF(0, 0, option.rect.width(), option.rect.height())
doc.drawContents(painter, clip)
painter.restore()
def sizeHint(self, option, index):
self.initStyleOption(option,index)
doc = QtGui.QTextDocument()
doc.setHtml(option.text)
doc.setTextWidth(option.rect.width())
return QtCore.QSize(doc.idealWidth(), doc.size().height())
class Widget(QtWidgets.QWidget, Ui_Form):
def __init__(self, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
self.setupUi(self)
self.listWidget.setItemDelegate(HTMLDelegate())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment