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. Get remaining part of a large string to fit fixed rectangle
QtWS25 Last Chance

Get remaining part of a large string to fit fixed rectangle

Scheduled Pinned Locked Moved Unsolved General and Desktop
font size
7 Posts 2 Posters 2.0k 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.
  • B Offline
    B Offline
    Borzh
    wrote on last edited by Borzh
    #1

    I have a very large string, a font and a rectangle to draw that string into. If the string doesn't fit, I would like to know the size of string that does fit into that rectangle... if the string does fit, then I would like to know bounding rectangle height. How to achieve this?

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

      hi have you read about
      http://doc.qt.io/qt-5.5/qpainter.html#drawText-5
      It can return a rectangle for the complete text.

      B 1 Reply Last reply
      0
      • mrjjM mrjj

        hi have you read about
        http://doc.qt.io/qt-5.5/qpainter.html#drawText-5
        It can return a rectangle for the complete text.

        B Offline
        B Offline
        Borzh
        wrote on last edited by
        #3

        @mrjj Yes, I have seen that. Unfortunately, it doesn't help in getting length of string that fits.

        mrjjM 1 Reply Last reply
        0
        • B Borzh

          @mrjj Yes, I have seen that. Unfortunately, it doesn't help in getting length of string that fits.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Borzh
          So say we have
          abcdefg and we can only display abc
          you want to know the sub string that was possible to draw
          given the rect?

          hmm, it reports back in pixels not letters.
          Sorry Im not aware of anything that would report back such info.

          B 1 Reply Last reply
          0
          • mrjjM mrjj

            @Borzh
            So say we have
            abcdefg and we can only display abc
            you want to know the sub string that was possible to draw
            given the rect?

            hmm, it reports back in pixels not letters.
            Sorry Im not aware of anything that would report back such info.

            B Offline
            B Offline
            Borzh
            wrote on last edited by Borzh
            #5

            @mrjj Yes, that is correct. Of course, I could create a method that acts like a binary search, halfing/doubling string length calling boundingRect(), until low margin == high margin, but I assume this is not very optimized.

            mrjjM 1 Reply Last reply
            0
            • B Borzh

              @mrjj Yes, that is correct. Of course, I could create a method that acts like a binary search, halfing/doubling string length calling boundingRect(), until low margin == high margin, but I assume this is not very optimized.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Borzh
              well, you could also do the word wrap yourself, using
              http://doc.qt.io/qt-5.5/qfontmetrics.html#details
              or look at source code for drawtext and see if you reuse some of it for a version
              that can return the substring.

              Is this big string with spaces ?

              B 1 Reply Last reply
              1
              • mrjjM mrjj

                @Borzh
                well, you could also do the word wrap yourself, using
                http://doc.qt.io/qt-5.5/qfontmetrics.html#details
                or look at source code for drawtext and see if you reuse some of it for a version
                that can return the substring.

                Is this big string with spaces ?

                B Offline
                B Offline
                Borzh
                wrote on last edited by
                #7

                I am posting my own binary search algorithm. BTW you can use QFontMetrics instead of QPainter if you want.

                int FontUtils::FittingLength(const QString& s, QPainter &p, const QRectF& rect,
                                             int flags/* = Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap*/)
                {
                    QRectF r = p.boundingRect(rect, flags, s);
                    if (r.height() <= rect.height()) // String fits rect.
                        return s.length();
                
                    // Apply binary search.
                    QString sub;
                    int left = 0, right = s.length()-1;
                    do
                    {
                        int pivot = (left + right)>>1; // Middle point.
                        sub = s.mid(0, pivot+1);
                        r = p.boundingRect(rect, flags, sub);
                
                        if (r.height() > rect.height()) // Doesn't fit.
                            right = pivot-1;
                        else
                            left = pivot+1;
                    } while (left < right);
                
                    left++; // Length of string is one char more.
                
                    // Remove trailing word if it doesn't fit.
                    if  ( !s.at(left).isSpace() && !s.at(left+1).isSpace() )
                    {
                        while ( (--left > 0) && !sub.at(left).isSpace() );
                        left++;
                    }
                
                    return left;
                }
                
                1 Reply Last reply
                1

                • Login

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