tags:

views:

329

answers:

2

Hello, i have a QTextEdit document which size is different by every print. In the middle of the document i have few blocks of text which are inseparable in the eyes of the user and i have to protect my blocks from splitting on two pages in case the document gets equivalent size. Have you got any solution?

A: 

The closest thing you have to something that does this is Unicode Paragraph Separator (U+2029). if you surround a body of text with paragraph separators then the renderer is not supposed to leave the first line or the last line of the paragraph on its own in a page. This behavior can be easily observed in MS-Word. If however the text of the block is four lines or more then you don't have a grantee that all four lines will be in the same page. they could split 2-2 between pages.

The concept you're talking about is widely used in a smaller scale between words and lines. If you put an NBSP (No-Break Space, U+00A0) between words instead of a normal space then the renderer knows that the line should not be broken in this space.
What you're actually looking for is a "No-Page-Break Line-Feed". Unfortunately Unicode does not have such a character.

What you can try is to write the whole block of text using NBSPs instead of spaces. Eventually your only-NBSP text is going to get larger than a line and it will have to break somewhere. this is a certain end-case of Unicode which I'm not sure is really defined. Perhaps there's a chance that the line break which will be generated is such that does not break between pages. This is even a further end-case to the end-case. The only way to know what happens in this situation is to try.

shoosh
You're right, but i don't know how to find sth like that in qt. The other idea is to measure the lenght of the document and use "\f" if the size is in the particular range.
Intersting info about unicode that I wasn't aware of, thanks.
Arnold Spence
+3  A: 

I am a great fan of Qt but I have not had an opportunity yet to use a QTextEdit. I would like to help though so I took a look through the documentation.

If you are using a sufficiently recent version of Qt you should find that a QTextEdit has an associated QTextDocument and it would seem that the functionality you seek may be there. A QTextDocument is a hierarchy of frames and blocks. A block is equivalent to a paragraph as you mention above.

The documentation seems to indicate that you could specify that your blocks be wrapped in a frame. Then, you should be able to set a format for the frame using QTextFrame::setFrameFormat. This format will allow you to specify a page break policy using some flags. The flags that are specified are PageBreak_Auto, PageBreak_AlwaysBefore and PageBreak_AlwaysAfter.

It would seem to me that if you set both the PageBreak_AlwaysBefore and PageBreak_AlwaysAfter flags for the frame, then your blocks should stay together.

I realize this is a fairly complex series. I hope that it helps and would really like to hear if it works.

Arnold Spence
Works perfect! Thank you! I don't know how did i miss it in the documentation, i'm ashamed:)
I'm glad it worked
Arnold Spence