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. Relation between QFonMetrics.width and QTextLayout.maximumWidth?

Relation between QFonMetrics.width and QTextLayout.maximumWidth?

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.6k 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.
  • E Offline
    E Offline
    equalsraf
    wrote on last edited by
    #1

    I'm painting a single line of text (no breaks) into a painter using QTextLayout, for example:

    @
    QFontMetrics fm(font);
    QTextLayout l(str);
    QTextOption opt;
    opt.setWrapMode(QTextOption::WrapAnywhere);
    l.setTextOption(opt);
    l.setFont(font);

    l.beginLayout();
    QTextLine line = l.createLine();
    line.setLineWidth(fm.width(str));
    l.endLayout();
    l.draw(&painter, pos);@

    Most of the times this will work, painting the text correctly. But sometimes it fails to draw the last char in the string. When it fails, the values for fm.width(str) and l.maximumWidth() are different by 1 pixel (the layout width is larger). If change to

    @
    line.setLineWidth(fm.width(str)+1);
    @

    then draws the entire string. Shouldn't the with of the QTextLayout be the same or less than the one provided by QFontMetrics?

    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