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. QStaticText::size() bug when using rich text and word wrap
QtWS25 Last Chance

QStaticText::size() bug when using rich text and word wrap

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.5k 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.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote on last edited by
    #1

    I currently use QPainter:drawText() to draw text inside QGraphicsItem::paint(). It works fine. But I would like to draw rich text, so I have changed QPainter:drawText() to QPainter:drawStaticText(). But I am now finding it impossible to position the text correctly because QStaticText::size() returns the wrong value when I am using QTextOption::WordWrap and Qt::RichText.

        QStaticText st( text );
        QTextOption options( align );
        options.setWrapMode( QTextOption::WordWrap );
        st.setTextOption( options );
        st.setTextFormat( Qt::RichText );
        st.setTextWidth( textRect.width() );
        st.prepare( p->worldTransform(), p->font() );
        QSizeF sz = st.size();
        QPointF pos( textRect.topLeft() );
        qreal hOffset = ( textRect.height() - sz.height() ) / 2.0;
        pos += QPointF( 0.0, hOffset );
        p->setClipRect( textRect );
        p->drawStaticText( pos, st );
    

    The height value returned QStaticText::size() doesn't match the height of the text displayed on screen. If I remove the word wrap or change it to plain text it works fine. But I need the word wrap and rich text.

    0_1528967748280_sizes.png

    Any ideas?

    I am using Qt 5.9.4 on Windows 7.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you try using QStaticText::setTextFormat ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • AndyBriceA Offline
        AndyBriceA Offline
        AndyBrice
        wrote on last edited by
        #3

        @AndyBrice said in QStaticText::size() bug when using rich text and word wrap:

        Yes, it is in the snippet above:

        st.setTextFormat( Qt::RichText );

        It works fine with Qt::PlainText. But that isn't useful for me.

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          My bad, I missed it.

          Do you also have that with the latest version of Qt ? It should be 5.11

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • AndyBriceA Offline
            AndyBriceA Offline
            AndyBrice
            wrote on last edited by
            #5

            I haven't tried it with 5.11. I tend to avoid the latest release as I value stability over the latest features. I guess I can report it as a bug and maybe they will tell me if/when it is finished. Where do you report Qt bugs these days?

            aha_1980A 1 Reply Last reply
            0
            • AndyBriceA AndyBrice

              I haven't tried it with 5.11. I tend to avoid the latest release as I value stability over the latest features. I guess I can report it as a bug and maybe they will tell me if/when it is finished. Where do you report Qt bugs these days?

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #6

              @AndyBrice

              at bugreports.qt.io

              it's nevertheless a good idea to test with the latest version first, in case that bug is already fixed (sometimes strange things happen ;))

              also please prepare a minimal example to reproduce your problem. otherwise it's hard for the devs to help you.

              thanks

              edit: and please provide a link to the report here.

              Qt has to stay free or it will die.

              1 Reply Last reply
              2
              • AndyBriceA Offline
                AndyBriceA Offline
                AndyBrice
                wrote on last edited by
                #7

                I am happy to report that this bug is fixed in Qt5.11.1 on Windows and Mac. Hooray!

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved