PDA

View Full Version : Hide/unhide paragraphs in QTextEdit



mcostalba
13th April 2007, 20:05
Hi,

I have some text to display in a QTextEdit and I would like to hide/unhide some parts on a toggle command.

Currently I use QSyntaxHighlighter to color some selected parts, I would like to know if there is a way to also hide that parts.

I have tried to setting font to 1 pixel size and also setting color to QTextEdit background but with no good results because, also if invisible the hidden part takes (empty) space so you still see an empty 'gap' in the text.

There is a way to achieve that?

Thanks
Marco

VireX
14th April 2007, 05:00
Why not just figure out where in the text you want to delete, and use TextCursor to delete that specific part, save that part in some variable or whatever, and then when you want to add it again, paste it again at the saved spot?

mcostalba
14th April 2007, 09:07
I would like to hide/unhide some parts of the text so that if I use two QString variables one for original text and one for shrinked text I have the problem to restore the screen at the right content, as example if I am at line 200 and hide the first 30 lines I would like still be at visible line 200 - 30 = 170 so that user does not see content scrolling artifact when hiding/unhiding.

To do this I hoped to find a supported way in QTextEdit because save and restore the contents positions is not easy (expecially in Qt4) and in any case I would need a map that says that line 200 of original content corresponds to line 170 of shrinked one and to do this for each line.

So a native way should be far better ;-)

VireX
14th April 2007, 18:22
Well obviously there is no hide unhide in QTextEdit, so you will have to just delete and repaste ... to imitate that.

fullmetalcoder
16th April 2007, 12:02
Well obviously there is no hide unhide in QTextEdit, so you will have to just delete and repaste ... to imitate that.
Deleting is OK as long as you keep what you delete somewhere... One simple way is probably to use a custom structure passed to QTextBlock::setUserData() which will store the temporarily hidden text in the way you like. This is especially interesting when what you call hide/unhide is actually "text folding" in this case the main drawback of this method becomes an advantage because the hidden data gets automatically deleted when corresponding blocks are removed. If you want some inspiration on how to do this you may consider having a look at QCodeEdit (http://www.qtcentre.org/forum/f-qt-software-16/t-qcodeedit-3178.html) which provides such text folding.