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. Does QFontMetricsF ::width() equals painter.drawText() width?

Does QFontMetricsF ::width() equals painter.drawText() width?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 114 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.
  • MozzieM Offline
    MozzieM Offline
    Mozzie
    wrote on last edited by
    #1

    Hi,
    I want to use QPainter to draw text and make sure the text center at the origin of the coordinate axis

    
    QFontMetricsF fm(m_graduateFont);
    qDebug() << "m_graduateFont" << m_graduateFont;
    qDebug() << "fm" << fm.size(Qt::TextSingleLine, "0");
    qDebug() << "fm" << fm.size(Qt::TextSingleLine, "11");
    qDebug() << "fm" << fm.size(Qt::TextSingleLine, "100");
    qDebug() << "fm" << fm.boundingRect(QRect(), Qt::AlignCenter, "100");
    double fontHeigth = fm.height();
    double textWidth = fm.width(num);
    painter.save();
    painter.setFont(m_graduateFont);
    painter.drawText(QPointF(- textWidth * .5, fontHeigth * .5), num);
    
    painter.setPen(Qt::red);
    painter.drawLine(-textWidth * .5, 0, textWidth * .5, 0);
    painter.drawLine(0, -fontHeigth * .5, 0, fontHeigth * .5);
    painter.restore();
    

    but it seems they do not match, did I forget something?

    3.PNG

    thanks.

    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