How to vertically center a single-line in a QTextEdit instance (PySide/PyQt)?

后端 未结 2 1699
故里飘歌
故里飘歌 2021-01-19 06:10

I have a line editor that inherits from QTextEdit, and I am using it to edit view items that show rich text. The second parameter for QTextEdit.setAlignme

2条回答
  •  猫巷女王i
    2021-01-19 06:55

    For a single-line edit centred vertically, you just need to calculate a correct fixed height.

    Using the example delegate from your previous question, it can be achieved like this:

    class RichTextLineEdit(QtGui.QTextEdit):   
        def __init__(self, parent=None):
            ...    
            margin = 1
            self.document().setDocumentMargin(margin)
            fontMetrics = QtGui.QFontMetrics(self.font())
            height = fontMetrics.height() + (margin + self.frameWidth()) * 2
            self.setFixedHeight(height)
    

    (NB: the reimplemented sizeHint and minimumSizeHint methods are probably redundant in the original example).

提交回复
热议问题