Highlight All line in QTextEdit



  • I use this code below to highlight the select text in text Browser.
    the var selectLine have a text to highlight, but i want select all line width in editor with this text not just a selected text

    @
    void MyHighlighter::highlightBlock(const QString &text)
    {
    QTextCharFormat myClassFormat;
    QColor backColor = QColor("Red");
    myClassFormat.setBackground(backColor);
    myClassFormat.setProperty(QTextFormat::FullWidthSelection, true);

    QString pattern = selectLine;
     
    QRegExp [removed]pattern);
    int index = text.indexOf(expression);
    while (index >= 0) {
    int length = expression.matchedLength();
    setFormat(index, length , myClassFormat);
    index = text.indexOf(expression, index + length);
    }
    }
    }
    

    @

    how can I do IT?



  • Here question is to find the index which is start position to highlight. For this you need to find the line which contains this text. According to me it is round about.

    1. Find the CursorPosition of the string using find(..). This is global cursor position
    2. From the cursor Position get the block
      block = cursor.block()
    3. From Block get the layout of the block
      QTextLayout lyt = block.layout()
    4. From Layout
      relpos = cursor.position() - block.position();
      QTextLine = lyt->lineForTextPosition(relpos);

    Now you need to have start position of line. Now you can use find(....) again to by passing entire textline itself as string.

    As of now this is striking to my mind. If any other simpler way some body can help as well.



  • thanks for your answer.
    But I my application I don't need get cursor pos, I set selected line with var selectLine.
    see image with this link below.
    http://i60.tinypic.com/21cb6zt.jpg


Log in to reply
 

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