Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, February 2008
How to modify a QTextDocument without affecting the undo/redo stacks?


Message 1 in thread

How to edit a QTextDocument without affecting the undo/redo stacks?

I wrote a syntax highlighter using QTextCursor into 
QTextDocument::contentsChanged(). Setting colors using QTextCursor works 
fine, but I experience a problematic side-effect of having the 
colorization being written as separate edit commands into the undo/redo 
stacks. This is Qt 4.3.3.

Any comments are welcome,
--
 [ signature omitted ] 

Message 2 in thread

There're QSyntaxHighlighter class and corresponding example in  
examples/richtext/syntaxhighlighter. I'd suggest using them.

If you still want to create your own highlighter, you may try this:
1) block signals from your editor (QObject::blockSignals(true));
2) apply colorization;
3) unblock signals (QObject::blockSignals(false)).

I haven't found better way to prevent undo/redo stack modifications  
because QTextDocument::setUndoRedoEnabled(false) clears it and that's  
definately not what you want.

On Wed, 06 Feb 2008 23:17:29 +0300, Frank Mertens <frank@xxxxxxxxxxx>  
wrote:
> How to edit a QTextDocument without affecting the undo/redo stacks?
>
> I wrote a syntax highlighter using QTextCursor into  
> QTextDocument::contentsChanged(). Setting colors using QTextCursor works  
> fine, but I experience a problematic side-effect of having the  
> colorization being written as separate edit commands into the undo/redo  
> stacks. This is Qt 4.3.3.
>
> Any comments are welcome,
> --
> Frank.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with  
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

-- 
 [ signature omitted ] 

Message 3 in thread

Thx, for that hint, had no idea about QObject::blockSignals().
Tried it, but it wasn't working.)

Found a cleaner solution:

QTextDocument => QxCxxHighlight::contentsChanged(int,int,int):
textCursor_->joinsPreviousEditBlock();
... apply color information
textCursor_->endEditBlock();

Cheers,
Frank.

Constantin Makshin wrote:
> There're QSyntaxHighlighter class and corresponding example in 
> examples/richtext/syntaxhighlighter. I'd suggest using them.
>
> If you still want to create your own highlighter, you may try this:
> 1) block signals from your editor (QObject::blockSignals(true));
> 2) apply colorization;
> 3) unblock signals (QObject::blockSignals(false)).
>
> I haven't found better way to prevent undo/redo stack modifications 
> because QTextDocument::setUndoRedoEnabled(false) clears it and that's 
> definately not what you want.
>
> On Wed, 06 Feb 2008 23:17:29 +0300, Frank Mertens <frank@xxxxxxxxxxx> 
> wrote:
>> How to edit a QTextDocument without affecting the undo/redo stacks?
>>
>> I wrote a syntax highlighter using QTextCursor into 
>> QTextDocument::contentsChanged(). Setting colors using QTextCursor 
>> works fine, but I experience a problematic side-effect of having the 
>> colorization being written as separate edit commands into the 
>> undo/redo stacks. This is Qt 4.3.3.
>>
>> Any comments are welcome,
>> -- 
>> Frank.
>>
>> -- 
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx 
>> with "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>
> --Constantin "Dinosaur" Makshin
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>

--
 [ signature omitted ]