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. Number of characters that fit into given width

Number of characters that fit into given width

Scheduled Pinned Locked Moved Unsolved General and Desktop
qfontmetricsqstring
4 Posts 2 Posters 1.3k 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.
  • A Offline
    A Offline
    Arthur Araruna
    wrote on last edited by
    #1

    I would like to be able to determine, given a string (QString) that is supposed to be displayed within a given width (qreal) area, how many of its characters would fit without overflowing that width.

    Is there any helper class / function that I can use to get this information, or do I need to create my own? If I do, what measurements should I include in this calculation? I have access to the QFont and QFontMetrics objects that will be used.

    Thank you.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Something like http://doc.qt.io/qt-5/qfontmetrics.html#elidedText ?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      A 1 Reply Last reply
      1
      • A Offline
        A Offline
        Arthur Araruna
        wrote on last edited by Arthur Araruna
        #3

        Maybe yes. Most likely to work.

        The problem would be if ::ElideNone does not work as expected... Also, it would be better if I didn't have to make another QString in order to get just the number of characters.

        But I'll give it a go.

        Thank you.

        1 Reply Last reply
        0
        • VRoninV VRonin

          Something like http://doc.qt.io/qt-5/qfontmetrics.html#elidedText ?

          A Offline
          A Offline
          Arthur Araruna
          wrote on last edited by Arthur Araruna
          #4

          @VRonin said in Number of characters that fit into given width:

          Something like http://doc.qt.io/qt-5/qfontmetrics.html#elidedText ?

          Turns out it will not work for me.

          The QString returned includes the ellipsis and there is no way to avoid them, because passing Qt::ElideNone means "do not truncate" instead of "omit the ellipsis" (as I feared).

          1 Reply Last reply
          0

          • Login

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