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. draw a string which fit all the width avaible
QtWS25 Last Chance

draw a string which fit all the width avaible

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 383 Views
  • 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.
  • D Offline
    D Offline
    dridk2
    wrote on last edited by
    #1

    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 ?

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 :)

      1 Reply Last reply
      4

      • Login

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