QPainter没有正确绘制文本

时间:2017-10-26 10:55:46

标签: c++ qt qt4

我无法使用QPainter drawText()方法绘制路径

QString Path = "/home/User"
QPainter *painter    
QRectF centerRect = QRectF (textX,centerY + m_bounds.height()*0.15, fm.width(Path),fm.height());
painter->drawText(centerRect,Path,QTextOption(Qt::AlignCenter | Qt::AlignHCenter  ));

在Windows中它绘制文本但在ubunthu Linux上只显示/ home

1 个答案:

答案 0 :(得分:0)

QFontMetrics fm = painter->fontMetrics();

qreal centerY = ( m_bounds.height()  -  fm.height() -10  )/2;
qreal textX = 8;

QRectF centerRect = QRectF (textX,centerY + m_bounds.height()*0.15, fm.width(m_folderPath),fm.height());
painter->drawText(centerRect,m_folderPath,QTextOption(Qt::AlignCenter | Qt::AlignHCenter  ));

fm.width(m_folderPath) 

显示/ home / user和/ homeuser4

的不同值