QGraphicsRectItem::paint() 内で、rect() 内にアイテムの名前を描画しようとしています。ただし、さまざまな項目ごとに幅が可変であり、同様に名前も長さが可変です。
現在、私は最大フォントサイズから始めて、それが収まるかどうかを確認し、収まるフォントサイズが見つかるまで減らしています。これまでのところ、これを行うための迅速かつ簡単な方法を見つけることができませんでした。これを行うためのより良い、またはより効率的な方法はありますか?
ありがとう!
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));
}
}