简体   繁体   中英

QFontMetrics.width() not returning the correct value in linux

QString folderPath = "/home/Users"
Q_UNUSED(option);
Q_UNUSED(widget);

painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);

painter->setPen(QPen(QColor(16,87,98),2));
painter->setBrush(Qt::NoBrush);

painter->drawRect(m_bounds);
m_pathItem->setPos(m_bounds.topLeft());

QFont font( "Calibri" );
font.setPixelSize(14);
font.setBold(false);
painter->setFont( font );

painter->setPen(QColor(16,87,98));

QFontMetrics fm = painter->fontMetrics();
fm.width(folderPath) // Returns 71 

When I used the below string pixel returned is different

QString folderPath = "/homeUsers!"
fm.width(folderPath) // Returns 73

This happens only in linux, windows its working fine

You're calculating the width of a text using a proportional font, where the characters / and ! may indeed have a different widths, so the total width of "/home/Users" may differ from "/homeUsers!".

Obviously this is not the case for the Windows font Calibri , which is available in Windows Vista and later. If you didn't install it in Linux a substitute is used, which may show the given behaviour.

Recommended reading: The detailed description of QFont . The important part is

Qt will use the font with the specified attributes, or if no matching font exists, Qt will use the closest matching installed font. The attributes of the font that is actually used are retrievable from a QFontInfo object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM