在 PyQt5 中重新实现 QStyledItemDelegate 绘制函数后更改 QTableView 的字体大小

发布于 2025-01-10 12:23:41 字数 2010 浏览 0 评论 0原文

我已经为 QTableView 实现了多行文本编辑器,如下所示: 使 QTableView 的行随着编辑器高度的增加而展开

我尚未解决的一个问题是完全控制绘制文本的样式。这里提供了字体颜色的解决方案: 在 PyQt5 中用 ItemDelegate QTextEdit 替换默认编辑器后更改 QTableView 的字体颜色

我已经为 QTableView 设置了样式表:

self.setStyleSheet(
    """font-size: 14px;
    gridline-color: rgb(60, 60, 60);"""
)

它适用于样式化编辑器以及网格。 理想情况下,我希望单元格与应用程序的其余部分具有相同的样式,同时可以通过某种方式对某些属性进行更改,从而影响编辑器和单元格的绘制方式。 到目前为止我为改变字体样式所做的一切都被忽略了。我尝试过更改 initStyleOption、更改画家的字体、更改选项的字体以及我能想到的所有其他内容。

这是当前的绘制功能:

def paint(self, painter, option, index):
    """
    Method override
    """
    # Remove dotted border on cell focus.  https://stackoverflow.com/a/55252650/3620725
    if option.state & QtWidgets.QStyle.State_HasFocus:
        option.state = option.state ^ QtWidgets.QStyle.State_HasFocus

    self.initStyleOption(option, index)
    painter.save()
    doc = QtGui.QTextDocument()
    doc.setDocumentMargin(3)
    doc.setTextWidth(option.rect.width())
    # changed to setPlainText from setHtml because setHtml was removing all newlines
    doc.setPlainText(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())
    painter.setClipRect(clip)

    layout = doc.documentLayout()
    ctx = layout.PaintContext()
    ctx.palette = option.palette
    layout.draw(painter, ctx)
    painter.restore()

程序运行时应用应用程序范围的样式表:

app = QApplication(sys.argv)
app.setStyleSheet(qtstylish.dark())

我相信编辑器和绘制的单元格之间的间距也不同。

比较

I have implemented a multiline text editor for a QTableView as seen here: Make row of QTableView expand as editor grows in height

One issue I have not yet been able to solve is getting full control over the styling of the painted text. A solution for the font color was provided here: Changing the font color of a QTableView after replacing the default editor with an ItemDelegate QTextEdit in PyQt5

I have set a stylesheet for the QTableView:

self.setStyleSheet(
    """font-size: 14px;
    gridline-color: rgb(60, 60, 60);"""
)

And it works for styling the editor as well as the grid.
Ideally, I would like the cells to have the same styling as the rest of the application while having some way of making changes to some of the attributes which would then affect both the editor and the way the cells are painted.
Everything I have done so far in order to change the styling of the font has been ignored. I have tried changing the initStyleOption, changing the painter's font, changing the option's font and everything else I could think of.

This is the current paint function:

def paint(self, painter, option, index):
    """
    Method override
    """
    # Remove dotted border on cell focus.  https://stackoverflow.com/a/55252650/3620725
    if option.state & QtWidgets.QStyle.State_HasFocus:
        option.state = option.state ^ QtWidgets.QStyle.State_HasFocus

    self.initStyleOption(option, index)
    painter.save()
    doc = QtGui.QTextDocument()
    doc.setDocumentMargin(3)
    doc.setTextWidth(option.rect.width())
    # changed to setPlainText from setHtml because setHtml was removing all newlines
    doc.setPlainText(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())
    painter.setClipRect(clip)

    layout = doc.documentLayout()
    ctx = layout.PaintContext()
    ctx.palette = option.palette
    layout.draw(painter, ctx)
    painter.restore()

An application wide stylesheet is applied when the program is run:

app = QApplication(sys.argv)
app.setStyleSheet(qtstylish.dark())

I believe the spacing is also different between the editor and the painted cell.

Comparison

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2025-01-17 12:23:42

当 QTextDocument 作为独立对象创建时,它只能使用应用程序默认值,包括字体。

请注意,使用全局样式表,即使使用通配符选择器,也不会为应用程序设置默认字体,而只会为小部件设置默认字体,并且由于 QTextDocument 和 QAbstractItemDelegate 不是小部件,不会应用样式表字体。

虽然无法访问样式表,但 paint() 函数的 style option 参数可以提供此类信息:该函数的参数将是该函数的默认样式选项。视图(viewOptions()),和只要 FontRole 返回有效的 QFont,initStyleOption() 最终就会改变字体。

解决方案是设置 defaultFont() 文档:

def paint(self, painter, option, index):
    # ...
    doc = QtGui.QTextDocument()
    doc.setDefaultFont(option.font)
    # ...

When a QTextDocument is created as a standalone object, it can only use the application defaults, including the font.

Note that using a global stylesheet, even with wildcard selectors does not set the default font for the application, but only for the widgets, and since QTextDocument and QAbstractItemDelegate are not widgets, no style sheet font will be applied.

While there is no access to stylesheets, the style option argument of the paint() function can provide such information: the argument of the function will be the default style option for the view (viewOptions()), and initStyleOption() will eventually alter the font as long as FontRole returns a valid QFont.

The solution is then to set the defaultFont() of the document:

def paint(self, painter, option, index):
    # ...
    doc = QtGui.QTextDocument()
    doc.setDefaultFont(option.font)
    # ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文