QtUser
QtUser

Reputation: 215

How to write Text with someother letter in Qt?

I want to write text with certain pattern,like suppose i want to write A with a's.Can anybody suggest how to achieve this in Qt.I got clue with plugandpaint example in Qt and that is actually painting on painter,but i want to draw a text with given letter. Please help me.

Upvotes: 2

Views: 333

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40492

Do you want something like that?

image

QBrush generate_letter_brush(QString text, QFont font) {
  QFontMetrics font_metrics(font);
  QPixmap pixmap(font_metrics.boundingRect(text).size());
  pixmap.fill(Qt::transparent);
  QPainter painter(&pixmap);
  painter.setFont(font);
  painter.drawText(pixmap.rect(),text);
  painter.end();
  return QBrush(pixmap);
}

QImage image(QSize(200, 200), QImage::Format_ARGB32);
image.fill(QColor(200, 255, 200));
QPainter painter(&image);
painter.setFont(QFont("", 80));
QBrush brush = generate_letter_brush("a", QFont("", 1));
painter.setPen(QPen(brush, 1));
painter.drawText(image.rect(), "A");
painter.end();
image.save(filename, "BMP"); //save
ui->label->setPixmap(QPixmap::fromImage(image)); //or display

Upvotes: 3

Related Questions