在QT4中实现方法:
QFontMetrics metr(m_Font);
int nWidth, nHeigh;
qreal rx, ry;
QPointF qPos, qpf;
QRectF rect; // m_rect为原始绘制区域
nWidth = metr.size(Qt::TextDontClip, strTemp).width();
nHeigh = metr.size(Qt::TextDontClip, strTemp).height();
rx = (qreal)m_rect.width() / (qreal)nWidth;
ry = (qreal)m_rect.height() / (qreal)nHeigh;
qPos = boundingRect().topLeft();
qpf = QPointF(m_rect.topLeft());
painter->translate(qpf);
painter->scale(rx, ry);
rect = QRectF(0, 0, m_rect.width(), m_rect.height());
painter->setPen(m_nClrText);
painter->drawText(rect, strTemp);
在QT5中实现有所不同:
QTextOption option;
option.setAlignment(m_euAlign); // 这里设置文字其他属性
option.setWrapMode(QTextOption::WrapMode::WrapAtWordBoundaryOrAnywhere);
painter->drawText(m_rect, strTemp, option);