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

后端 未结 2 1677
故里飘歌
故里飘歌 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条回答
  •  广开言路
    2021-01-19 06:47

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

提交回复
热议问题