简体   繁体   English

如何在委托中为粗体文本重新实现sizeHint? (QT)

[英]How to reimplement sizeHint for bold text in a delegate? (Qt)

Short Version 简洁版本

Running resizeColumnsToContents works fine for regular text, but when I change text to bold, the columns are too narrow. 对于常规文本,运行resizeColumnsToContents效果很好,但是当我将文本更改为粗体时,列太窄。 How can I fix sizeHint in my custom delegate to take into account that my text is bold? 如何在我的自定义委托中修复sizeHint以考虑到我的文本为粗体?

Details 细节

I am using a custom delegate to make the text in a column bold. 我正在使用自定义委托使列中的文本变为粗体。 To do this, in the paint method of the delegate, in the column of interest I just run option.font.setWeight(QtGui.QFont.Bold) . 为此,请在委托的paint方法中,在感兴趣的列中运行option.font.setWeight(QtGui.QFont.Bold)

When resizing columns to contents, however, the column with bolded text is too narrow. 但是,当将列的大小调整为内容时,加粗文本的列太窄。 I tried to reimplement sizeHint in the delegate as follows, but it yields columns that are the exact same size as the default implementation: 我尝试按以下方式在委托中重新实现sizeHint ,但它产生的列与默认实现的大小完全相同:

def sizeHint(self, option, index):
    fontMetrics = option.fontMetrics  
    if index.column() == 0:
        text = index.model().data(index)
        document = QtGui.QTextDocument(text)
        document.setDefaultFont(option.font)
        return QtCore.QSize(document.idealWidth(), fontMetrics.height())
    return QtGui.QStyledItemDelegate.sizeHint(self, option, index)         

So, how can I get the sizeHint to correctly identify the width of my emboldened font? 那么,如何获得sizeHint来正确识别加粗字体的宽度? It seems I am simply sending in plain text here. 看来我只是在这里以纯文本格式发送。

Related Questions 相关问题

QLabel sizehint is too small QLabel sizehint太小

Based on the comment by Pie_Jesu, I inserted option.font.setWeight(QtGui.QFont.Bold) before calculating the width. 基于Pie_Jesu的评论,我在计算宽度之前插入了option.font.setWeight(QtGui.QFont.Bold) So sizeHint ends up as follows: 因此sizeHint最终如下所示:

def sizeHint(self, option, index):
    fontMetrics = option.fontMetrics  
    if index.column() == 0:
        text = index.model().data(index)
        document = QtGui.QTextDocument(text)
        option.font.setWeight(QtGui.QFont.Bold) #new line
        document.setDefaultFont(option.font)
        return QtCore.QSize(document.idealWidth(), fontMetrics.height())
    return QtGui.QStyledItemDelegate.sizeHint(self, option, index)    

So, basically, whatever properties you set with the text in your paint function, one way to match that width within sizeHint would be to explicitly build in those same properties. 因此,基本上,无论您在paint函数中使用文本设置的属性是什么,在sizeHint匹配该宽度的一种方法都是显式地构建相同的属性。

It works, but it seems like suspicious code reduplication to set the text properties twice, once in paint and then again in sizeHint . 它可以正常工作,但是似乎可疑的代码重复处理将文本属性设置两次,一次是在paint ,再一次是在sizeHint There should be a more modular way. 应该有一个更加模块化的方式。 That is, a way for sizeHint to directly read the formatting from the item being displayed. 也就是说, sizeHint可以从正在显示的项目中直接读取格式。

Edit added later: that's pretty much how these things work. 稍后再添加编辑:这几乎就是这些东西的工作方式。 On the plus size, by using QTextDocument , you are only setting up a document, not attaching it to a QTextEdit for display, so there is actually not much overhead here. QTextDocument ,通过使用QTextDocument ,您仅设置文档,而没有将其附加到QTextEdit进行显示,因此实际上这里没有太多开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM