Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextFormat::FullWidthSelection but for multiple lines?
Qt 6.12 LTS is out! See what's new in the release blog

QTextFormat::FullWidthSelection but for multiple lines?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 684 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.
  • T Offline
    T Offline
    Trapezoid_Dreams
    wrote on last edited by
    #1

    The Code Editor Example shows how to highlight the full width of the current line: https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html

    It does this using QTextFormat::FullWidthSelection as follows:

    QList<QTextEdit::ExtraSelection> extraSelections;
    
        if (!isReadOnly()) {
            QTextEdit::ExtraSelection selection;
    
            QColor lineColor = QColor(Qt::yellow).lighter(160);
    
            selection.format.setBackground(lineColor);
            selection.format.setProperty(QTextFormat::FullWidthSelection, true);
            selection.cursor = textCursor();
            selection.cursor.clearSelection();
            extraSelections.append(selection);
        }
    
        setExtraSelections(extraSelections);
    

    For things that display as multiple lines due to word wrapping, I would like it to highlight every line that is a part of the current block (I think that is the right terminology for it). But QTextFormat::FullWidthSelection doesn't seem to do that. It's only highlighting the current line, one line at a time.

    I tried highlighting the block as a whole instead, but that's not the same behavior as highlighting the full width of the line(s) themselves and I'm struggling to find if there's a way to manually recreate the selection behavior of QTextFormat::FullWidthSelection but for an entire block.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Trapezoid_Dreams
      wrote on last edited by
      #2

      I think I figured out a solution. Could probably be a bit cleaner, but this seems to work ok so far. Basically am iterating through the block's lines and do full width selection for each.

      QTextCursor cursor = textCursor();
      cursor.clearSelection();
      cursor.movePosition(QTextCursor::StartOfBlock);
      for (int i = 0; i < cursor.block().lineCount(); i++)
      {
          QTextEdit::ExtraSelection selection;
      
          QColor lineColor = QColor(Qt::lightGray);
      
          selection.format.setBackground(lineColor);
          selection.format.setProperty(QTextFormat::FullWidthSelection, true);
          selection.cursor = cursor;
          extraSelectionList.append(selection);
      
          if (i + 1 < cursor.block().lineCount())
              cursor.movePosition(QTextCursor::Down);
      }
      
      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
      • Unsolved