tags:

views:

154

answers:

2

Is there any other way to change the QTextLayout of a QTextBlock that is within a QTextDocument without having to subclass QAbstractTextDocumentLayout and call its documentChanged?

I know that on a call to QTextBlock::layout() const ; "the returned QTextLayout object can only be modified from the documentChanged implementation of a QAbstractTextDocumentLayout subclass" but I was wodering if there was any other way before I implemented it.

UPDATE basically I just want paragraphs(QTextBlock) that are longer than X characters to be highlighted and the rest to be normal.

+1  A: 

Problem with changing block's layout directly is that it will inadvertently affect the layout of the currently active document layout (instance of the internal QAbstractTextDocumentLayout implementation) - it computes positions of QTextDocument elements only inside the documentChanged() and uses them whenever it needs to repaint or hit-test - so these things might get broken.

Can you elaborate on what you're trying to achieve?

im trying to highlight paragraphs that are longer than 15 characters to indicate they are too long. I am also trying to do this inside a QPlainTextEditor object from a slot connected to either the cursorPositionChanged() or textChanged() signals. I have tried textCursor().block().blockFormat().setBackground(QBrush(Qt::yellow); textCursor().block().layout()->setFont(QFont("Helvetica [Cronyx]")); and textCursor().setBlockFormat(*blkFormat); all without success but I have received a post on another question that mentions using QTextEdit::mergeCurrentCharFormat() so im going to try it.
yan bellavance
From what you are telling me though it might be safer to just use QAbstractTextDocumentLayout::documentChanged ( int position, int charsRemoved, int charsAdded ). The other question I asked is here:http://stackoverflow.com/questions/2512661/how-to-change-the-background-of-a-text-bloc-in-a-text-document-in-qt
yan bellavance
+1  A: 

Look at QSyntaxHighlighter http://doc.qt.nokia.com/4.6/qsyntaxhighlighter.html

This pretty much will let you do exactly what you're trying to do.

Brian Roach