Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. text width using font family and font size
Forum Updated to NodeBB v4.3 + New Features

text width using font family and font size

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 2.5k Views 1 Watching
  • 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
    AYYAPPA
    wrote on 30 Jan 2018, 10:17 last edited by AYYAPPA
    #1

    hello ,

    i am trying to find the maximum width required to fit a text with specific font family and font size.

    qt is not returning exact width for text, and it seems not considering font family also.

    here is my code in cpp

    int UIUtilities::findTextWidth(const QString &text, const QString &fontName, int fontSize, int maxWidth) const
    {
    QFont myFont(fontName, fontSize);;
    QString str(text);
    QFontMetrics fm(myFont);
    int width=fm.width(str);
    if(width < maxWidth) return width;
    else return maxWidth;
    }

    here is the calling line in qml.

    width: UIUtil.findTextWidth("hello", "Garamond", 16,100 )
    width: UIUtil.findTextWidth("hello", "SomeRandomName....", 16,100 )

    above both values are same!!
    does qt really identifies which family we are referring to recognize width of a character?, as width of character varies with different font families even with same pixel size.

    thanks,
    ayyappa

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 30 Jan 2018, 10:52 last edited by
      #2

      Qt will automatically fall back to known fonts when you try to use a non-existing font. So it is very probable that you get the same value because Qt fell back to the same font behind the scenes.

      Try to first validate that the font you requested really exists and was loaded.

      (Z(:^

      1 Reply Last reply
      2
      • A Offline
        A Offline
        AYYAPPA
        wrote on 30 Jan 2018, 12:17 last edited by AYYAPPA
        #3

        how can i validate the font.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 30 Jan 2018, 12:46 last edited by
          #4

          This QFont::exactMatch() or you can check against installed families in QFontDatabase.

          (Z(:^

          1 Reply Last reply
          0

          1/4

          30 Jan 2018, 10:17

          • Login

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