How to use QTextCursor to highlight a text

  • hello guys

    my aim is to highlight some letters in a text. I made a class inheriting from QSyntexhighlighter but i don't know how to set the cursor. Currently i wait for calling updatefn from inside the highlightblock but this seems to time costing much computations and time. Could any body help me ?


    void Highlighter::highlightBlock(const QString &text)
    int startIndex = 0;
    if (previousBlockState() != 1)
    startIndex = commentStartExpression.indexIn(text);
    qDebug() << text << "block";
    //QTextBlock block = new QTextBlock (this);
    //int blockNumber = block.blockNumber();
    if (!text.isEmpty())
    //qDebug() << text << "linenumber=" << blockcounter;
    blockcounter ++; //line counter

    void Highlighter::updatefn(int length) //mistakes
    //update when the block number is the one given in the list
    // last added correctionblock is
    //qDebug() << "text block count" <<

    QTextCharFormat MyMistakeFormat;
    int i;
    int mist_size; int conc_size; int corr_size;
    int max;
    mist_size=mistakes.size(); conc_size=concentrations.size(); corr_size=correct.size();
    max=(mist_size>=conc_size ? mist_size : conc_size); // if mist>conc return mist else conc
    max=(corr_size>=max ? corr_size : max); // if corr>max return corr else max
    int a;
    for (i=1; i<=max; i++)
        if (i<=mist_size )
            if (blockcounter==mistakesBlock[a])  //mistakesBlock.last())
                //qDebug() << "update - blocks = " << blockcounter <<  "correction last value=" &lt;&lt; mistakesBlock.last();
    //QTextCursor cur(parent);
    //qDebug() &lt;&lt; "cursor position" << cur.position() << "inblock" << cur.positionInBlock();


  • Why would you want to set the cursor inside the QSyntaxHighlighter? I suppose you'd want to do that on the QTextEdit that the highlighter was operating on, i.e.

    QTextEdit* myTextEdit = new QTextEdit(parent);
    Highlighter* myHighlighter = new Highlighter(myTextEdit); // this assigns the highligter to the text edit


    QTextCursor cursor = myTextEdit->textCursor();
    cursor.setPosition(myPosition); // use desired position here


    Hope this helps.

  • I want to set the cursor because I want to automatically change color of wrong letters.
    So I found that to change a certain color of a letter, it must be selected by cursor.

    Am I right?

  • I'm not sure when the highlighter code gets called. If you change the cursor there then it might interfere with the user trying to type something. Perhaps do it after the user has stopped typing for a certain amount of time?

  • yes, after the user types, there is an error checking phase .. where the user does not write any thing .. like spelling mistakes function from microsoft winword .
    But I don't know what is a block and how to set the position of a cursos. how to set myPosition in the code?

  • So far as I used it, myPosition is just the index of the character in the total string. So if there is an error say 5 characters from the beginning of the string then I'd set myPosition to 4.

    As for what a block is, I always get the entire text edit text in one highlightBlock() call so I never went into that.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.