在我的QGraphicsRectItem :: paint()中,我试图在它的rect()中绘制该项目的名称。但是,对于每个不同的项目,它们可以具有可变的宽度,并且类似的名称可以具有可变的长度。对于Qt 4.6.x,如何自动调整文本大小以适应指定的宽度?
目前我以最大字体大小开始,检查它是否适合并递减,直到找到适合的字体大小为止。到目前为止,我还没有找到一个快速简单的方法来做到这一点。有没有更好或更有效的方法来做到这一点?
谢谢!
void checkFontSize(QPainter *painter, const QString& name) {
// check the font size - need a better algorithm... this could take awhile
while (painter->fontMetrics().width(name) > rect().width()) {
int newsize = painter->font().pointSize() - 1;
painter->setFont(QFont(painter->font().family(), newsize));
}
}
参见[这个问题](http://stackoverflow.com/q /1329652分之36575192)。 – 2016-11-29 14:28:53