[Qt-interest] removing all user block data of a QTextEdit

Lorenzo Bettini bettini at dsi.unifi.it
Sun May 17 20:57:20 CEST 2009


Hi

in a text editor I'm using my own implementation of syntax highlighting
(deriving from QSyntaxHighlighter), which sets user block data in the
QTextDocument.

Now, I need to switch among highlighters, and when I switch the
highlighter, the previous set user block data must be removed.

Is there a smarter way then this one (which saves the text, clears it,
and then resets it)?

void qeditexample::changeHighlightingLanguage(const QString &newLang) {
     // avoid to switch language if it's just the same
     if (newLang.toStdString() == highlighter->getLangFile())
         return;

     // make sure we remove all the information in the QTextBlocks
     // that were added by the previous highlighter
     QString text = textEdit->toPlainText();
     textEdit->clear();

     // set the new Qt4SyntaxHighlighter for highlighting context
     highlighter =
               new srchiliteqt::Qt4SyntaxHighlighter(textEdit->document());
     highlighter->init(newLang.toStdString());

     // reset the text, that will be highlighted with the new scheme
     textEdit->setText(text);
}

-- 
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134     (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com  http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net




More information about the Qt-interest-old mailing list