[Qt-interest] Emulating selection with offsets

Stephen Jackson spjackson42 at gmail.com
Tue Apr 21 15:36:01 CEST 2009


>
> Hello all,
>
> I have a text file. I only have startOffset and endOffset of a section of
> text in that file.
>
> I need to highlight that section.
>
> I used the below approach but failed.
>
>  Please guide me.
>
>
>
> QTextCursor *cur = new QTextCursor();
>
> textEditorArray[textEditorCount]->setTextCursor(*cur);
>
> cursor->setPosition (startOffset , QTextCursor::MoveAnchor );
>
> cursor->movePosition ( QTextCursor::NextCharacter, QTextCursor::KeepAnchor,
> endOffset - startOffset);
>
> textEditorArray[textEditorCount]->setFontUnderline(true);
>

The following code does the sort of thing you want when added to the
on_findButton_clicked function in

examples/uitools/textfinder/textfinder.cpp

    QTextCursor selectCursor(document);
    int startOffset = 10;
    int endOffset   = 15;
    selectCursor.setPosition ( startOffset , QTextCursor::MoveAnchor );

    selectCursor.setPosition ( endOffset , QTextCursor::KeepAnchor );

    ui_textEdit->setTextCursor(selectCursor);

Note that setTextCursor takes a *copy* of the cursor, so there is not
point calling it *before* you attempt to set the selection, as you are
doing above.

Hope this helps,

Stephen Jackson



More information about the Qt-interest-old mailing list