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

QTextLayout::maximumWidth() is not correct for multi-line text

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2: Important P2: Important
    • 6.5.0 Beta1
    • 5.15.2, 6.2.4, 6.3.1
    • GUI: Text handling
    • None
    • All
    • 013c346a8d (qt/qtbase/dev) 013c346a8d (qt/tqtc-qtbase/dev) 70e33a585c (qt/qtbase/6.4) 70e33a585c (qt/tqtc-qtbase/6.4) 07bde1cadf (qt/tqtc-qtbase/6.2) 991c056438 (qt/tqtc-qtbase/dev)

      Found this while investigating QTBUG-89557.

      QTextLayout::maximumWidth() doesn't take into account line separator for multi-line text.
      Reproduced on the current Dev as well.

      Code sample:

      #include <QGuiApplication>
      #include <QTextLayout>
      
      qreal layoutMaximumWidth(const QString &text)
      {
          QTextLayout layout(text);
          layout.beginLayout();
          while (layout.createLine().isValid()) { }
          layout.endLayout();
      
          return layout.maximumWidth();
      }
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          qreal w1 = layoutMaximumWidth("test");
          qDebug() << "singleline maxwidth:" << w1;
      
          qreal w2 = layoutMaximumWidth("test\ntest");
          qDebug() << "multiline maxwidth:" << w2;
      
          Q_ASSERT(qFuzzyCompare(w1, w2));
      
          return 0;
      }
      

      Output:

      singleline maxwidth: 19.4688
      multiline maxwidth: 38.9375
      ASSERT: "qFuzzyCompare(w1, w2)" in file ..\textLayout\main.cpp, line 24
      

      For the test case above w1 should be equal to w2, and the ASSERT should not fire.
      See full example textLayout.zip
       

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            esabraha Eskil Abrahamsen Blomfeldt
            studiosus Vladimir Belyavsky
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: