PDA

View Full Version : QTextEdit: truncating text to fit window



anja.ende
3rd July 2012, 21:55
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,

Anja

mentalmushroom
4th July 2012, 07:49
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.

high_flyer
4th July 2012, 10:46
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.