Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use QTextCursor to highlight a text
QtWS25 Last Chance

How to use QTextCursor to highlight a text

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherifOmran
    wrote on 2 Oct 2012, 19:07 last edited by
    #1

    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";
    */
    //Blockcount
    //QTextBlock block = new QTextBlock (this);
    //int blockNumber = block.blockNumber();
    if (!text.isEmpty())
    {
    //qDebug() << text << "linenumber=" << blockcounter;
    updatefn(1);
    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;
         MyMistakeFormat.setFontWeight(QFont::Bold);
         MyMistakeFormat.setForeground(Mistake_color);
         MyMistakeFormat.setBackground(Mistake_color_background);
    
    
    
    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++)
    {
        a=i-1;
        if (i<=mist_size )
        {
            if (blockcounter==mistakesBlock[a])  //mistakesBlock.last())
            {
                //qDebug() << "update - blocks = " << blockcounter <<  "correction last value=" &lt;&lt; mistakesBlock.last();
                setFormat(mistakes[a],length,MyMistakeFormat);
                setCurrentBlockState(0);
            }
       }
    
    
    }
    
    
    
    //QTextCursor cur(parent);
    //cur.setPosition(CurrentLine);
    //qDebug() &lt;&lt; "cursor position" << cur.position() << "inblock" << cur.positionInBlock();
    

    }
    @

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rogier
      wrote on 10 Oct 2012, 08:12 last edited by
      #2

      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
      myTextEdit->ensureCursorVisible();

      @

      Hope this helps.
      Regards,
      Rogier

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on 10 Oct 2012, 11:57 last edited by
        #3

        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?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rogier
          wrote on 10 Oct 2012, 11:59 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SherifOmran
            wrote on 10 Oct 2012, 12:02 last edited by
            #5

            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?

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rogier
              wrote on 10 Oct 2012, 12:07 last edited by
              #6

              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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved