Need underline ONE word in QPlainTextEdit [SOLVED]
-
I need underline only one word in text. I know it's line number and position in line. I do not want use syntax highlighter for this simple task. Just tried this code (Editor class is subclass of QPlainTextEdit):
@void Editor::setErrorPosition( unsigned int line, unsigned int pos )
{
QTextCursor cursor = textCursor();
cursor.movePosition( QTextCursor::Start );
cursor.movePosition( QTextCursor::Down, QTextCursor::MoveAnchor, line - 1 );
cursor.movePosition( QTextCursor::StartOfLine );
cursor.movePosition( QTextCursor::Right, QTextCursor::MoveAnchor, pos );
cursor.movePosition( QTextCursor::WordRight, QTextCursor::KeepAnchor );// proper word selected - that was tested QTextCharFormat defcharfmt = currentCharFormat(); QTextCharFormat newcharfmt = defcharfmt; newcharfmt.setUnderlineColor( QColor( Qt::red ) ); newcharfmt.setUnderlineStyle( QTextCharFormat::WaveUnderline ); setCurrentCharFormat( newcharfmt ); // oops... no underline appears cursor.movePosition( QTextCursor::WordLeft ); // remove selection and set cursor to begining setCurrentCharFormat( defcharfmt ); // return default char format without underline
}@
but the underline did not appear. Even if remove latest two lines and try type - characters appear without underline. What is wrong here? I read following in docs:
bq. void QPlainTextEdit::setCurrentCharFormat ( const QTextCharFormat & format )
Sets the char format that is be used when inserting new text to format by calling QTextCursor::setCharFormat() on the editor's cursor. If the editor has a selection then the char format is directly applied to the selection.and expected that selected text will be underlined. May be I missed some setting anywhere, but which one exactly?
-
I noticed that there is no call of underline allowing method. Just included
@ newcharfmt.setFontUnderline( true );@
before line 14 in the code. But this did not help. Even no any other font setting works. Tried make selection italic or else. Nothing works here. What is wrong? May be it doesn't work because of syntax highlighter is attached to this text?
ANYBODY HELP?
-
Two comments:
remember that you're working on a COPY of the text cursor. You have to set it back before the calls to setCurrentCharFormat, or the QPlainTextEdit will not have the cursor's selection and thus that call won't do what you want;
you could just use QTextCursor::setCharFormat directly instead. Again, if you move the cursor (line 19), you MUST set it back.
-
thank you that's was proper idea...
@ cursor.movePosition( QTextCursor::Start );
cursor.movePosition( QTextCursor::Down, QTextCursor::MoveAnchor, line - 1 );
cursor.movePosition( QTextCursor::StartOfLine );
cursor.movePosition( QTextCursor::Right, QTextCursor::MoveAnchor, pos );
cursor.movePosition( QTextCursor::WordRight, QTextCursor::KeepAnchor );
setTextCursor( cursor ); // added
QTextCharFormat defcharfmt = currentCharFormat();
QTextCharFormat newcharfmt = defcharfmt;
newcharfmt.setFontUnderline( true );
newcharfmt.setUnderlineColor( QColor( Qt::red ) );
newcharfmt.setUnderlineStyle( QTextCharFormat::WaveUnderline );
newcharfmt.setFontItalic( true );
setCurrentCharFormat( newcharfmt );
cursor.movePosition( QTextCursor::WordLeft );
setTextCursor( cursor ); // added
setCurrentCharFormat( defcharfmt );
setFocus();@works fine
-
Damn... With this underlining I suddenly lost syntax highlighting. :-( After call of setCurrentCharFormat( newcharfmt ); at line 13 all text except current line is shown without syntax highlighting. What the?...
Syntax highlighting is implemented as recommended in docs and examples:
@Syntaxer::Syntaxer( ... ) : QSyntaxHighlighter(parent)
{
... setting rules for highlighting ...
}void Syntaxer::highlightBlock(const QString &text;)
{
foreach( HighRule rule, highlightRules )
{
... setting formats if words match rules ...
}
}@exactly that's all. It works fine until call of setCurrentCharFormat(). After this call QtGUI engine redraws only current line (current block) with syntax highlighting. Other lines it redraws without highlighting - just black on white text. Of course each next changed line appears with highlighting.
I don't know what to do with that. GUI engine calls highlightBlock() method, not me. I tried update() or repaint() text after setCurrentCharFormat() but without effect.
PLEASE ANYBODY HELP RETURN HIGHLIGHTING!
-
For me this looks more like a small bug. I could not imagine situation where syntax highlighting in other blocks except current should be lost after character format change. May be in some exotic case it should. But I'm pretty sure by default it must keep highlighting. Syntax highlighting exists mostly for programming languages. Each of them requires highlighting in entire source code. For exotic cases there could be special bool parameter in QSyntaxHighlighter settings. Something like setHLcurrentBlock( bool b = false )
-
Bugs are not covered in the docs. You could file a bugreport in the "bugtracker.":https://bugreports.qt.nokia.com/secure/Dashboard.jspa
If you feel your experience/workaround can help others, please add it as a doc note.