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 QTextBrowser highlight line?
Qt 6.11 is out! See what's new in the release blog

How QTextBrowser highlight line?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.4k 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    How to create a function to highlight line from the given line number?

    QTextBrowser.highlightLine(i);

    https://github.com/sonichy

    raven-worxR 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      You can try something like

      QTextCursor coursor(textBrowser->document()->findBlockByLineNumber(i));
      QTextBlockFormat frmt = coursor.blockFormat();
      frmt.setBackground(QBrush(Qt::yellow));
      coursor.setBlockFormat(frmt);
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      sonichyS 1 Reply Last reply
      3
      • sonichyS sonichy

        How to create a function to highlight line from the given line number?

        QTextBrowser.highlightLine(i);
        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @sonichy
        you can use setExtraSelections()

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        2
        • VRoninV VRonin

          You can try something like

          QTextCursor coursor(textBrowser->document()->findBlockByLineNumber(i));
          QTextBlockFormat frmt = coursor.blockFormat();
          frmt.setBackground(QBrush(Qt::yellow));
          coursor.setBlockFormat(frmt);
          
          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on last edited by sonichy
          #4

          @VRonin
          Your method can work, but highlight lines one by one, I need only highlight current line i, I use it to highlight song lyrics.
          And can you change the font color but not background color?

          https://github.com/sonichy

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Me and @raven-worx gave you the two solution based on usage. if you want the highlight to change based on user clicking on the line use @raven-worx solution. if you want the highlight as permanent or changing based on a state (a timer, an event, a button click) then you can use my solution

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            sonichyS 1 Reply Last reply
            0
            • VRoninV VRonin

              Me and @raven-worx gave you the two solution based on usage. if you want the highlight to change based on user clicking on the line use @raven-worx solution. if you want the highlight as permanent or changing based on a state (a timer, an event, a button click) then you can use my solution

              sonichyS Offline
              sonichyS Offline
              sonichy
              wrote on last edited by sonichy
              #6

              @VRonin
              Clear highlight has been solved.
              But how to change to highlight font color not background?
              QTextBlockFormat.setForeground(QBrush(Qt::yellow)) has no effort.

              // clear
              for(int a=0;a<lyrics.size();a++){
                  QTextCursor cursor(ui->textBrowser->document()->findBlockByLineNumber(a));
                  QTextBlockFormat TBF = cursor.blockFormat();
                  TBF.setBackground(QBrush(Qt::transparent));
                  cursor.setBlockFormat(TBF);
              }
              // color it
              QTextCursor cursor1(ui->textBrowser->document()->findBlockByLineNumber(i));
              QTextBlockFormat TBF1 = cursor1.blockFormat();
              TBF1.setBackground(QBrush(Qt::yellow));
              cursor.setBlockFormat(TBF1);
              ui->textBrowser->setTextCursor(cursor1);

              https://github.com/sonichy

              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