- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到了以下代码,我稍作修改,作为 how to use HTML formatted text in a QListWidget 的答案.
main.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>341</width>
<height>161</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>321</width>
<height>141</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="lwOptions"/>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
测试.py
import os
import html
from PyQt5 import uic
from PyQt5.Qt import (QApplication, QDialog, QListWidget,
QListWidgetItem, QStyleOptionViewItem,
QStyledItemDelegate, QStyle)
from PyQt5.QtGui import QPalette, QAbstractTextDocumentLayout, QTextDocument
from PyQt5.QtCore import QSize
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 = QStyleOptionViewItem(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))
else:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.Text))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None)
if index.column() != 0:
textRect.adjust(5, 0, 0, 0)
constant = 4
margin = (option.rect.height() - options.fontMetrics.height()) // 2
margin = margin - constant
textRect.setTop(textRect.top() + margin)
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
def sizeHint(self, option, index):
return QSize(self.doc.idealWidth(), self.doc.size().height())
class GUI(QDialog):
def __init__(self):
super(GUI, self).__init__()
dirname = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(dirname,'main.ui'), self)
delegate = HTMLDelegate(self.lwOptions)
self.lwOptions.setItemDelegate(delegate)
for ordinal in ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth']:
item = QListWidgetItem('This is the <b>{}</b> word. This is the <b>{}</b> word. This is the <b>{}</b> word.'.format(ordinal, ordinal, ordinal))
self.lwOptions.addItem(item)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = GUI()
window.show()
sys.exit(app.exec_())
代码工作正常,但是,长度超过 QListWidget
宽度的文本会被截断,并且不会显示水平滚动条。(需要时会自动添加垂直滚动条。)
我尝试通过 ScrollBarAlwaysOn 强制显示,它只显示一个不起作用的水平滚动条。
我需要如何更改代码才能在需要时自动添加水平滚动条?
最佳答案
我在之前的答案中进行的计算不正确,因为返回 sizeHint()
的大小是相对于前一项的。一个简单的解决方案是使用 Qt::SizeHintRole
角色来设置大小。
import os
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class HTMLDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent=None):
super(HTMLDelegate, self).__init__(parent)
self.doc = QtGui.QTextDocument(self)
def paint(self, painter, option, index):
painter.save()
options = QtWidgets.QStyleOptionViewItem(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(QtWidgets.QStyle.CE_ItemViewItem, options, painter)
ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
if option.state & QtWidgets.QStyle.State_Selected:
ctx.palette.setColor(QtGui.QPalette.Text, option.palette.color(
QtGui.QPalette.Active, QtGui.QPalette.HighlightedText))
else:
ctx.palette.setColor(QtGui.QPalette.Text, option.palette.color(
QtGui.QPalette.Active, QtGui.QPalette.Text))
textRect = style.subElementRect(QtWidgets.QStyle.SE_ItemViewItemText, options, None)
if index.column() != 0:
textRect.adjust(5, 0, 0, 0)
constant = 4
margin = (option.rect.height() - options.fontMetrics.height()) // 2
margin = margin - constant
textRect.setTop(textRect.top() + margin)
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
s = QtCore.QSize(self.doc.idealWidth(), self.doc.size().height())
index.model().setData(index, s, QtCore.Qt.SizeHintRole)
class GUI(QtWidgets.QDialog):
def __init__(self):
super(GUI, self).__init__()
dirname = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(dirname,'main.ui'), self)
delegate = HTMLDelegate(self.lwOptions)
self.lwOptions.setItemDelegate(delegate)
for ordinal in ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth']:
item = QtWidgets.QListWidgetItem('This is the <b>{}</b> word. This is the <b>{}</b> word. This is the <b>{}</b> word.'.format(ordinal, ordinal, ordinal))
self.lwOptions.addItem(item)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = GUI()
window.show()
sys.exit(app.exec_())
关于python - QStyledItemDelegate 水平截断文本并且不添加水平滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53600849/
我正在使用Qt 4.7。 我有一个模型,该模型在两列的QTableView中显示,我的目标是在QTableView中提供对该模型的内联编辑。 +-----------------+----------
我有一个代表MyDelegate用于QListWidget .委托(delegate)派生自 QStyledItemDelegate . MyDelegate 的目标之一是在ListWidget 的每
我有一个 QTreeWidget,我想通过使用样式委托(delegate)来完全自定义项目的外观。 我的主要问题是我想在我的项目右侧创建一个自定义按钮,它允许我折叠和展开该项目的子项。经典的“+”按钮
我得到了以下代码,我稍作修改,作为 how to use HTML formatted text in a QListWidget 的答案. main.ui Dialog
我目前正在尝试研究模型 View 方法并编写缩略图查看器应用程序。 在此示例中,我只是尝试绘制 20 个框,但我得到的似乎是随机选择,该选择会随着鼠标移动而更新。滚动使事情变得更糟,并且绘制有时只有框
我是 Python 和 PyQt5 的新手。我正在使用 QStyledItemDelegate 制作仅包含 ComboBox 的 QTableView 列之一。我设法显示了 ComboBox,但我在处
我创建了一个 QStyledItemDelegate 类,我想在其中使一些项目可检查,一些项目带有两个小部件。但它不能正常工作。我错过了什么?这是它的样子: 请看第 1 行,看起来两个小部件在那里,但
在我的一个项目中,我使用 QTableWidget 来显示一些复杂的计算结果。为了提高表格的可读性,我需要在单个表格单元格内显示两个对齐的值。 稍后我想通过使用颜色或箭头等进一步自定义小部件。 为此,
我正在使用 PySide2,但我找不到任何关于如何在 QStyledItemDelegate 子类中使用 paint() 函数的文档。我对类(class)相当陌生,但到目前为止可以理解,但在 PySi
我正在使用双调度创建一个样式化的 QTreeView 来解析数据项的特定委托(delegate),这非常有效。我对 QStyledItemDelegate 的委托(delegate)进行了子类化,以利
我想绘制一个遵循当前样式的自定义项目委托(delegate)。但“WindowsVista/7”风格和“WindowsClassic”风格在文本颜色上存在差异。 我使用以下代码来绘制背景(工作): v
我正在尝试使 QStyledItemDelegate 的行为类似于我编写的自定义 QWidget,因此我可以将我的代码切换到模型/ View 方法。 自定义 QWidget 是一个复杂的按钮,鼠标悬停
在我的项目中,我继承了 QStyledItemDelegate 并从 createEditor 函数返回了一个自定义编辑器。 QWidget* TagEditDelegate::createEdito
我有一个 QTableView,我正在为其设置自定义 QStyledItemDelegate。 除了自定义项绘制之外,我还想为选择/悬停状态设置行的背景色样式。我想要的外观类似于此 KGet 屏幕截图
我有一个 QStyledItemDelegate 派生对象用于 QTableView 派生 View 。我根据模型索引数据类型进一步委托(delegate)绘画和编辑器创建。对于 bool,我想通过复
我想在 QTableWidget 中显示两列,显示两个字符串之间的差异(之前由一些 Levenshtein 距离算法计算)。这些部分存储在每个 QTableWidgetItem 的数据中,作为 QSt
当鼠标悬停在项目委托(delegate)中的文本上时,如何更改鼠标图标?我有这部分,但我找不到任何更改鼠标指针的示例。 我错过了什么? void ListItemDelegate::paint(
我正在尝试使用 Qt,并希望根据模型的值以自定义文本颜色显示模型。以颜色呈现它是一个可选设置,所以我想避免在我的模型中使用 Qt::ForegroundRole,而是在 QStyledItemDele
我应该如何使用委托(delegate)更改我的树项目的背景颜色? 如果不在文本顶部绘画,我一直无法弄清楚如何做到这一点。换句话说,当我使用下面的代码时,颜色绘制在文本之上。文本在背景下... def
我试图使当前代码显示的所有内容都不可编辑。 之前的搜索都建议要么修改模型的flags()函数,要么使用表的setEditTriggers。我在这段代码中同时执行了这两项操作,但它们都不起作用。 查看逐
我是一名优秀的程序员,十分优秀!