Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-136696

Surprising behavior of QTextCharFormat::setForeground(Qt::NoBrush)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.2.4, 6.10
    • GUI: Text handling
    • None
    • Ubuntu 24.04 LTS

    Description

      1. In a QTextLayout, assign a pen for the text's outline and Qt::NoBrush for the text's foreground:
            QTextLayout textLayout("Qt");
            QTextLayout::FormatRange range;
            range.start = 0;
            range.length = 2;
            range.format.setFont(f);
            range.format.setTextOutline(pen);
            range.format.setForeground(Qt::NoBrush);
            textLayout.setFormats(QList<QTextLayout::FormatRange>{range});
        
      2. Draw this using a QPainter:
        textLayout.draw(&p, QPoint(0, 200));
        

        Expected: The drawn characters will not be filled, i.e. inside the outline, they will be transparent:

        Actual: The drawn characters are filled with the color of the outline's pen:

      I attached a small project to reproduce the issue. It also draws a rectangle for comparison.

      Workaround: Setting the foreground to Qt::transparent draws the expected shape.

      Attachments

        1. actual.png
          actual.png
          7 kB
        2. CMakeLists.txt
          0.6 kB
        3. expected.png
          expected.png
          11 kB
        4. main.cpp
          1 kB
        5. text_invisible.svg
          0.6 kB

        Issue Links

          For Gerrit Dashboard: QTBUG-136696
          # Subject Branch Project Status CR V

          Activity

            People

              esabraha Eskil Abrahamsen Blomfeldt
              rlohning Robert Löhning
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There is 1 open Gerrit change