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

痴心易碎 提交于 2019-12-01 22:31:08

问题


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.setAlignment is `QtAligntment' and the docs say:

Valid alignments are Qt.AlignLeft, Qt.AlignRight, Qt.AlignJustify and Qt.AlignCenter (which centers horizontally).

That is, there is no native support for vertical alignment. Is there an indirect way to vertically center the text in QTextEdit?

Related link

Center the Text of QTextEdit horizontally and vertically : Unfortunately, the accepted answer uses QLineEdit which won't work for me.

A clue?

At the following I found a clue about how to do it in C++/Qt. I am almost able to follow it, but not quite, as it is for c++:

http://www.qtcentre.org/threads/26003-Vertical-centering-of-a-QTextEdit

I will hack at it on my own for a couple of days and try to answer it myself, but wanted to post this now in case someone has already cracked it already or done it in a different/better way.


回答1:


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).




回答2:


While the accepted answer works for default font size, it breaks when I change the font size or vertical margins (see comments). The text line edit class below centers the text vertically, for all font sizes and vertical margins that I've tested.

It sets up the editor using QTextDocument which is then assigned to the QTextEdit instance. QTextDocuments provide the back-end containers for QTextEdits anyway, and have built-in functionality for handling font sizes and margins, and give an additional layer of control over the editor.

In practice, I found using QTextDocument let me solve the problem in a more intuitive way without having, you don't have to delve into the nitty-gritty mechanics of frame widths, font metrics, and all that, which we did when working solely using native QTextEdit methods.

Note it uses setViewportMargins() instead of setContentMargins() (which is what you might expect it to use) because the latter is for setting margins for something that is inserted into a layout. The following editor is a standalone widget, not put into any layout, so setContentMargins() won't do anything.

import sys
from PySide import QtGui, QtCore

class TextLineEdit(QtGui.QTextEdit):
    topMarginCorrection = -4 #not sure why needed    
    returnPressed = QtCore.Signal()
    def __init__(self, fontSize = 10, verticalMargin = 2, parent = None):
        QtGui.QTextEdit.__init__(self, parent)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose) 
        self.setLineWrapMode(QtGui.QTextEdit.NoWrap)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setFontPointSize(fontSize)
        self.setViewportMargins(-verticalMargin, self.topMarginCorrection , 0, 0)  #left, top, right, bottom
        #Set up document with appropriate margins and font
        document = QtGui.QTextDocument()
        currentFont = self.currentFont()
        currentFont.setPointSize(fontSize)
        document.setDefaultFont(currentFont)
        document.setDocumentMargin(verticalMargin)  
        self.setFixedHeight(document.size().height())
        self.setDocument(document)

    def keyPressEvent(self, event):
        '''stops retun from returning newline'''
        if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
            self.returnPressed.emit()
            event.accept()
        else:
            QtGui.QTextEdit.keyPressEvent(self, event)


def main():
    app = QtGui.QApplication(sys.argv)
    myLine = TextLineEdit(fontSize = 15, verticalMargin = 8)
    myLine.show()    
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()


来源:https://stackoverflow.com/questions/32941367/how-to-vertically-center-a-single-line-in-a-qtextedit-instance-pyside-pyqt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!