void MyWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.setPen(Qt::red); painter.setFont(QFont("Arial", 20)); painter.drawText(QRect(10, 10, 100, 100), Qt::AlignCenter, "Hello World"); }
QImage img(100, 100, QImage::Format_RGB32); QPainter painter(&img); painter.setPen(Qt::blue); painter.setFont(QFont("Helvetica", 14)); painter.drawText(QPointF(10, 50), "Qt rocks!");
void MyWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.translate(100, 100); painter.rotate(45); painter.setFont(QFont("Arial", 20)); painter.drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, "Hello World"); }In this example, we create a QPainter object and translate it to position (100, 100) and rotate it by 45 degrees. We then set the font to Arial with size 20 and draw the text "Hello World" at the center of a 100x100 bounding box with -50,-50 offset due to the rotation. Package library: Qt