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. Highlight All line in QTextEdit
Forum Updated to NodeBB v4.3 + New Features

Highlight All line in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rzsis
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rzsis
        wrote on last edited by
        #3

        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

        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