Results 1 to 4 of 4

Thread: Vertical centering in QTextEdit is off by 4 pixels

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2014
    Posts
    98
    Platforms
    Windows
    Thanks
    43
    Thanked 4 Times in 4 Posts

    Wink Vertical centering in QTextEdit is off by 4 pixels

    I have a QTextEdit that I'm using as a single line editor for some tree views: it lets you set the font size and vertical margins for the vertically centered text. The code is pasted below (note it is PySide). I set up the font and margins using a QTextDocument, and then assign that document to the QTextEdit instance. It works great at all margin and font size combinations, but there is one thing driving me nuts: the text is not actually correctly centered on the widget unless I shift it up by four pixels using setViewportMargins. Why?

    Could it be related to the fact that QTextDocuments have default margins of 4? I doubt it, because I am manually setting the margins of the document.

    Any ideas?

    (Before you ask, I'm not using QLineEdit because I'm displaying html in my tree views...)

    SSCCE
    Qt Code:
    1. import sys
    2. from PySide import QtGui, QtCore
    3.  
    4. class TextLineEdit(QtGui.QTextEdit):
    5. topMarginCorrection = -4 #not sure why needed
    6. returnPressed = QtCore.Signal()
    7. def __init__(self, fontSize = 10, verticalMargin = 2, parent = None):
    8. QtGui.QTextEdit.__init__(self, parent)
    9. self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
    10. self.setLineWrapMode(QtGui.QTextEdit.NoWrap)
    11. self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    12. self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    13. self.setFontPointSize(fontSize)
    14. self.setViewportMargins(-verticalMargin, self.topMarginCorrection , 0, 0) #left, top, right, bottom
    15. #Set up document with appropriate margins and font
    16. document = QtGui.QTextDocument()
    17. currentFont = self.currentFont()
    18. currentFont.setPointSize(fontSize)
    19. document.setDefaultFont(currentFont)
    20. document.setDocumentMargin(verticalMargin)
    21. self.setFixedHeight(document.size().height())
    22. self.setDocument(document)
    23.  
    24. def keyPressEvent(self, event):
    25. '''stops return from returning newline'''
    26. if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
    27. self.returnPressed.emit()
    28. event.accept()
    29. else:
    30. QtGui.QTextEdit.keyPressEvent(self, event)
    31.  
    32.  
    33. def main():
    34. app = QtGui.QApplication(sys.argv)
    35. myLine = TextLineEdit(fontSize = 15, verticalMargin = 8)
    36. myLine.show()
    37. sys.exit(app.exec_())
    38.  
    39.  
    40. if __name__ == "__main__":
    41. main()
    To copy to clipboard, switch view to plain text mode 
    Last edited by neuronet; 2nd January 2016 at 19:08.

Similar Threads

  1. Vertical centering of a QTextEdit
    By miwarre in forum Newbie
    Replies: 4
    Last Post: 5th October 2015, 05:01
  2. hangup vertical scroll bar in qtextedit
    By mero in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2011, 18:59
  3. How to draw vertical text via QTextEdit
    By nifei in forum Qt Programming
    Replies: 8
    Last Post: 1st October 2010, 17:10
  4. Text block centering in Qt4.4 QTextEdit
    By mla1290 in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2008, 17:46
  5. Finidng the pixels for a character in QTextEdit
    By sukanyarn in forum Qt Programming
    Replies: 1
    Last Post: 27th October 2006, 09:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.