Results 1 to 4 of 4

Thread: Vertical centering in QTextEdit is off by 4 pixels

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

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Vertical centering in QTextEdit is off by 4 pixels

    The line of text quite probably is centred but that line also includes the inter-line spacing and/or ascender/descender heights (even if there are neither of these in the actual text). You might be able to adjust out the inter-line space with QTextBlockFormat.

  3. #3
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: Vertical centering in QTextEdit is off by 4 pixels

    Quote Originally Posted by ChrisW67 View Post
    The line of text quite probably is centred but that line also includes the inter-line spacing and/or ascender/descender heights (even if there are neither of these in the actual text). You might be able to adjust out the inter-line space with QTextBlockFormat.
    It is unlikely to have to do with ascender/descender heights because these depend on font size, and the 4 pixel correction works for every font size I have tried (from like 8 to 50). Line spacing would also depend on font size right?

    I've never used text blocks, that could give me more flexibility. I'm more just curious where these four pixels are coming from so I can better understand the code before releasing it: it is working very well right now so don't actually want to mess with it.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Vertical centering in QTextEdit is off by 4 pixels

    Line spacing would also depend on font size right?
    Maybe, maybe not. Have you done the experiment?

    QFontMetrics::leading() for the font might give you a size to work with.
    You should also look at the margin functions in QTextBlockFormat and setting a line height of type QTextBlockFormat::FixedHeight equal to the available space in the "line" edit.

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.