def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) return QSize(doc.idealWidth(), doc.size().height())
def f(): doc = QTextDocument() tables = lookup(line.text()) doc.setHtml( template.render(request={"script_root": ""}, tables=tables, url_for=lambda *_, **__: None)) doc.setTextWidth(doc.size().width()) pixmap = QPixmap(doc.size().width(), doc.size().height()) pixmap.fill(QColor(0, 0, 0, 0)) painter = QPainter(pixmap) doc.drawContents(painter) painter.end() lab.setPixmap(pixmap) lab.setFixedSize(pixmap.size())
def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex): """ Displays dirty files in red with corresponding rebuild buttons if in developer mode (is_dev_mode). Otherwise, renders normally Args: painter (QPainter): Current painter option (QStyleOptionViewItem): Current option index (QModelIndex): Current index of related model """ if self.is_dev_mode: source_model = self.get_source_model(index.model(), self.source_model_type) model = index.model() # get data of filename filename = str( model.data( model.sibling(index.row(), source_model.FILENAME, index))) if source_model.is_file_dirty(filename): if index.column() == source_model.FILENAME: text = filename palette = option.palette document = QTextDocument() document.setDefaultFont(option.font) document.setHtml(f"<font color={'red'}>{text}</font>") background_color = palette.base().color() painter.save() painter.fillRect(option.rect, background_color) painter.translate(option.rect.x(), option.rect.y()) document.drawContents(painter) painter.restore() elif index.column() == source_model.REBUILD: if '.py' in filename: text = "rebuild" palette = option.palette document = QTextDocument() # needed to add Right Alignment: qt bug : # https://bugreports.qt.io/browse/QTBUG-22851 document.setTextWidth(option.rect.width()) text_options = document.defaultTextOption() text_options.setTextDirection(Qt.RightToLeft) document.setDefaultTextOption( text_options) # get right alignment document.setDefaultFont(option.font) document.setHtml( f'<font color={"red"}> <b> {text}</b> </font>') background_color = palette.base().color() painter.save() painter.fillRect(option.rect, background_color) painter.translate(option.rect.x(), option.rect.y()) document.drawContents(painter) painter.restore() else: QItemDelegate.paint(self, painter, option, index) else: QItemDelegate.paint(self, painter, option, index)