垂直居中项目';使用委托创建文本(PySide/Qt/PyQt)

垂直居中项目';使用委托创建文本(PySide/Qt/PyQt),qt,pyqt,pyside,Qt,Pyqt,Pyside,我有一个用于QTableView的自定义委托,该委托允许显示/编辑html字符串(使用QTextDocument)。下面有一个SSCCE 不幸的是,当我使用paint()显示文本时,它并没有垂直居中,但似乎是顶部对齐的。例如,如果我将委托应用于第二列,而不是第一列,则该表如下所示: 我的搜索没有揭示如何从代理内部以原则性的方式解决此问题。手动添加5代码> >选项.Rec.y.() >在我的计算机上工作,但我不认为有原则。 有没有办法使文本垂直居中 SSCCE from PySide impor

我有一个用于
QTableView
的自定义委托,该委托允许显示/编辑html字符串(使用
QTextDocument
)。下面有一个SSCCE

不幸的是,当我使用
paint()
显示文本时,它并没有垂直居中,但似乎是顶部对齐的。例如,如果我将委托应用于第二列,而不是第一列,则该表如下所示:

我的搜索没有揭示如何从代理内部以原则性的方式解决此问题。手动添加5代码> >选项.Rec.y.() >在我的计算机上工作,但我不认为有原则。 有没有办法使文本垂直居中

SSCCE

from PySide import QtGui
import sys

class HtmlTable(QtGui.QTableView):
    def __init__(self, parent = None):    
        QtGui.QTableView.__init__(self)
        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels(['Title', 'Summary'])
        item0 = [QtGui.QStandardItem('Infinite Jest'), QtGui.QStandardItem('Hello, <i>Hal</i>')]
        item00 = [QtGui.QStandardItem('Hamlet'), QtGui.QStandardItem('Best served <b>cold</b>')]
        model.appendRow(item0)
        model.appendRow(item00)          
        self.setModel(model)
        self.setItemDelegate(HtmlPainter(self))

class HtmlPainter(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None):
        QtGui.QStyledItemDelegate.__init__(self, parent)
    def paint(self, painter, option, index):
        if index.column() == 1: 
            text = index.model().data(index) #default role is display
            palette = QtGui.QApplication.palette()
            document = QtGui.QTextDocument()
            document.setDefaultFont(option.font)
            #Set text (color depends on whether selected)
            if option.state & QtGui.QStyle.State_Selected:  
                displayString = "<font color={0}>{1}</font>".format(palette.highlightedText().color().name(), text) 
                document.setHtml(displayString)
            else:
                document.setHtml(text)
            #Set background color
            bgColor = palette.highlight().color() if (option.state & QtGui.QStyle.State_Selected)\
                     else palette.base().color()
            painter.save()
            painter.fillRect(option.rect, bgColor)
            painter.translate(option.rect.x(), option.rect.y())  #If I add +5 it works
            document.drawContents(painter)
            painter.restore()
        else:
            QtGui.QStyledItemDelegate.paint(self, painter, option, index)          


def main():
    app = QtGui.QApplication(sys.argv)
    myTable = HtmlTable()
    myTable.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
从PySide导入QtGui
导入系统
类HtmlTable(QtGui.QTableView):
def uuu init uuu(self,parent=None):
QtGui.QTableView.\uuuu init\uuuuu(self)
model=QtGui.QStandardItemModel()
model.setHorizontalHeaderLabels(['Title','Summary']))
item0=[QtGui.QStandardItem('Infinite Jest'),QtGui.QStandardItem('Hello,Hal')]
item00=[QtGui.QStandardItem('Hamlet'),QtGui.QStandardItem('Best served cold')]
model.appendRow(item0)
模型附录行(item00)
self.setModel(model)
self.setItemDelegate(htmlInter(self))
类HtmlPainter(QtGui.QStyledItemDelegate):
def uuu init uuu(self,parent=None):
QtGui.QStyledItemDelegate.\uuuuu init\uuuuu(self,parent)
def油漆(自身、油漆工、选项、索引):
如果index.column()==1:
text=index.model().data(index)#默认角色为display
palete=QtGui.QApplication.palete()
document=QtGui.QTextDocument()
document.setDefaultFont(option.font)
#设置文本(颜色取决于是否选中)
如果选择option.state和QtGui.QStyle.state\u:
displayString=“{1}”。格式(palette.highlightedText().color().name(),文本)
document.setHtml(显示字符串)
其他:
document.setHtml(文本)
#设置背景色
bgColor=palete.highlight().color()如果(option.state&QtGui.QStyle.state_选中)\
else调色板.base().color()
保存
painter.fillRect(option.rect,bgColor)
painter.translate(option.rect.x(),option.rect.y())#如果我加上+5,它就会工作
文件.图纸内容(油漆工)
恢复
其他:
QtGui.QStyledItemDelegate.paint(self、painter、option、index)
def main():
app=QtGui.QApplication(sys.argv)
myTable=HtmlTable()
myTable.show()
sys.exit(app.exec_())
如果名称=“\uuuuu main\uuuuuuuu”:
main()

您应该使用
QTextDocument::setTextWidth
设置文档的宽度。这允许您确定文字高度并使用它计算偏移:

document.setTextWidth(option.rect.width())
offset_y = (option.rect.height() - document.size().height())/2
painter.translate(option.rect.x(), option.rect.y() + offset_y) 
document.drawContents(painter) 
设置文本宽度也是必要的,因为否则当列不够宽时,文本将不会被包装


您可能需要重新实现
sizeHint
以根据文档大小计算首选宽度和高度。

Voila!是的,我有一个实现了
sizeHint
的完整示例。我只是想把我的SSCCE缩减到最基本的部分。现在这个问题似乎已经解决了,我必须检查一下我是否做对了