Relation between QFonMetrics.width and QTextLayout.maximumWidth?



  • 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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.