I need to draw a label onto a box.
Ideally I'd scale the label with the size of the box, but I'm not sure if there is any built in functionality for this kind of scaling.
At present I'm scaling the object to the height of the bounding box, but I'm not certain how to implement the width scaling because the width of the drawn text depends on the specific order of symbols (due to kerning).
Is there some built in functionality for this kind of scaling?
void total_control_roi_item::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget)
{
QGraphicsRectItem::paint(painter, option, widget);
painter->save();
const auto rect = boundingRect();
auto font = painter->font();
auto height_of_box = rect.height()*0.7;
font.setPointSizeF(height_of_box);
painter->setFont(font);
const auto label = QString("%1").arg(id_);
painter->drawText(rect, label, Qt::AlignHCenter | Qt::AlignVCenter);
painter->restore();
}
You can make a text escalation using the information of QFontMetrics.