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下它工作正常。
发布于 2017-10-27 16:20:07
您正在使用比例字体计算文本的宽度,其中字符/和!的宽度可能确实不同,因此"/home/Users“的总宽度可能不同于"/homeUsers!”。
显然,这不是Windows字体的情况,它在Windows Vista和更高版本中可用。如果你没有在Linux中安装它,那么将使用substitute,它可能会显示给定的行为。
推荐阅读:QFont的详细描述。最重要的部分是
Qt将使用具有指定属性的字体,或者如果不存在匹配的字体,Qt将使用最接近匹配的已安装字体。实际使用的字体的属性可以从QFontInfo对象中检索。
https://stackoverflow.com/questions/46955179
复制相似问题