簡體   English   中英

QFontMetrics.width()在Linux中未返回正確的值

[英]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 

當我使用下面的字符串像素時返回的是不同的

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

這僅在linux中發生,Windows正常工作

您正在使用比例字體(其中的字符/!計算文本的寬度! 可能確實具有不同的寬度,因此“ / home / Users”的總寬度可能與“ / homeUsers!”不同。

顯然,Windows Vista和更高版本中沒有提供Windows字體Calibri 如果您沒有在Linux中安裝它,則使用替代品 ,它可能會顯示給定的行為。

推薦閱讀: QFont的詳細描述。 重要的部分是

Qt將使用具有指定屬性的字體,或者如果不存在匹配的字體,則Qt將使用最接近的匹配安裝字體。 可以從QFontInfo對象檢索實際使用的字體的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM