Results 1 to 4 of 4

Thread: Maximum number of characters in width

  1. #1
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Maximum number of characters in width

    For various reasons, I need to be able to do the following:
    Given a String, a Font, and a width, what is the largest number of characters I can take from the front of the string that can be rendered within that width?

    Inefficient implementation:
    int i;
    for (i = 1; i < str.length(); ++ i)
    if (metric.width(str.left(i)) > max_width)
    return i - 1;
    return i;

    This could be improved by using a binary chop based method, but that still would take log(n) tries.

    Is there a method to get the answer with a single pass over the data?

    (It would ultimately look like
    rem_width -= char_width(str[0]);
    for (i = 1; i < str.length() && rem_width >= 0; ++ i)
    rem_width -= pair_width (str[i-1], str[i]);
    Or call a function that ultimately did that.)

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Maximum number of characters in width

    See QFontMetrics::boundingRect() (the one with QRect or width & height parameters).
    Last edited by jacek; 26th May 2008 at 02:36.

  3. #3
    Join Date
    May 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Maximum number of characters in width

    That's not what I want. The function I'd like to see would return an integer (number of characters), not a rectangle.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Maximum number of characters in width

    Quote Originally Posted by ralphmerridew View Post
    That's not what I want. The function I'd like to see would return an integer (number of characters), not a rectangle.
    Indeed, that's what happens when you answer questions at 3 A.M.

    Then maybe you will find something in QTextLayout?

Similar Threads

  1. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.