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"

    +----------------WIDH----------------+
    A         C           G            T  // string A
    ACGTATGTAGTAGTAGTAGTAGTAGATTTGTATATTG // string B
    

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


  • Qt Champions 2017

    Hi
    You can control the space with
    http://doc.qt.io/qt-5/qfont.html#setKerning
    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);
      painter.setFont(font);
    

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


Log in to reply
 

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