Results 1 to 3 of 3

Thread: QTextEdit: truncating text to fit window

  1. #1

    Default QTextEdit: truncating text to fit window

    Hi everyone,

    I am using a QTextEdit to display some text. Sometimes, the text is longer than the visible viewing area and what I would like to do is to truncate the text to the last word that fits in the viewable area without the scroll panes. I would like to add something like "More" at the end of the text and when the user clicks on it, it does some action.

    I figured out how to handle the clicking and highlighting by over riding the mouse events but i cannot figure out how to adjust the text so that the right amount is loaded into the QTextEdit.

    I would be very grateful if someone can suggest a way for this.

    Many thanks,


  2. #2
    Join Date
    Feb 2011
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4 Qt5
    MacOS X Windows

    Default Re: QTextEdit: truncating text to fit window

    I think, you should play with QFontMetrics. It helps to estimate the size of characters and strings. So you could try splitting your text by words, estimate size of one word and if it fits try two words etc.

  3. #3
    Join Date
    Jan 2006
    Munich, Germany
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows

    Default Re: QTextEdit: truncating text to fit window

    Not sure if the Qt API offers more "out of the box" solution, check the docs.
    If it doesn't here is what I would do:
    Subclass QTextEdit. (or create an event filter class to)
    filter the events of the scroll bars to catch when they want to be shown, and don't allow it - instead put your "more..." text in the text edit.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. how to get the text from QTextEdit
    By qt_user in forum Qt Programming
    Replies: 5
    Last Post: 22nd August 2011, 00:22
  2. how QTextEdit transparent to his parent window?
    By wesley in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2009, 07:23
  3. QTextEdit::text()
    By hazardpeter in forum Newbie
    Replies: 2
    Last Post: 5th August 2009, 21:13
  4. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  5. QTextEdit -> add Text
    By ape in forum Newbie
    Replies: 16
    Last Post: 19th December 2007, 14:59


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.