Nominate our 2022 Qt Champions!

draw a string which fit all the width avaible

  • Hi,
    I would like to draw a string (using QPainter) which use all the width avaible by changing caracter spaces.
    For exemple, with string A = "ACGT" and string B = "ACGTATGTAGTAGTAGTAGTAGTAGATTTGTATATTG"

    A         C           G            T  // string A

    Do you think there is a helper function to do that ? With paintText ? QTextOption ? AlignementFlags ?

  • Lifetime Qt Champion

    You can control the space with
    so if you not require it to be perfect, you can quick make such function

    QString StrSample = "TestMe" ;
      QRect r = QRect(0, 0, 190, 30);
      QFont font = painter.font();
      QFontMetrics fm(font);
      int tw = fm.width(StrSample);
      int diff = r.width() - tw;
      qreal lettersp = font.letterSpacing();
      qreal delta = diff / ( StrSample.length() - 1);
      font.setLetterSpacing(QFont::AbsoluteSpacing, lettersp + delta);

    There might be a Qt function that does it but im not stumbled upon it :)

Log in to reply