1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| void Label::paintEvent(QPaintEvent *e) { QPainter painter(this);
painter.save(); QFont font = painter.font(); font.setPixelSize(24); painter.setFont(font);
painter.drawText(QPoint(0, 20), tr("HelloHello"));
const QRect rectangle = QRect(0, 30, 100, 50); QRect boundingRect; painter.drawText(rectangle, 0, tr("HelloHello"), &boundingRect);
QPen pen = painter.pen(); pen.setStyle(Qt::DotLine); pen.setColor(Qt::red); painter.setPen(pen); painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width()));
pen.setStyle(Qt::DashLine); pen.setColor(Qt::green); painter.setPen(pen); painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width())); qDebug() << "boundingRect1:"<< boundingRect;
painter.restore();
{ painter.save(); font.setBold(true); painter.setFont(font);
painter.drawText(QPoint(0, 120), tr("HelloHello"));
const QRect rectangle = QRect(0, 130, 100, 50); QRect boundingRect; painter.drawText(rectangle, 0, tr("HelloHello"), &boundingRect);
QPen pen = painter.pen(); pen.setStyle(Qt::DotLine); pen.setColor(Qt::red); painter.setPen(pen); painter.drawRect(boundingRect.adjusted(0, 0, -pen.width(), -pen.width()));
pen.setStyle(Qt::DashLine); pen.setColor(Qt::green); painter.setPen(pen); painter.drawRect(rectangle.adjusted(0, 0, -pen.width(), -pen.width())); qDebug() << "boundingRect2:"<< boundingRect; painter.restore(); } }
|