3

I believe QPainter is used, but I can't figure out how to combine the two.

QLabel* imageLabel = new QLabel();
QImage image("c://image.png");
imageLabel->setPixmap(QPixmap::fromImage(image));
imageLabel->setAlignment(Qt::AlignCenter);

QPainter* painter = new QPainter();
painter->setPen(Qt::blue);
painter->setFont(QFont("Arial", 30));
painter->drawText(rect(), Qt::AlignCenter, "Text on Image");

1 Answers1

7

You need to tell the painter where to draw:

QImage image("c://image.png");

// tell the painter to draw on the QImage
QPainter* painter = new QPainter(&image); // sorry i forgot the "&"
painter->setPen(Qt::blue);
painter->setFont(QFont("Arial", 30));
// you probably want the to draw the text to the rect of the image
painter->drawText(image.rect(), Qt::AlignCenter, "Text on Image");

QLabel* imageLabel = new QLabel();
imageLabel->setPixmap(QPixmap::fromImage(image));
imageLabel->setAlignment(Qt::AlignCenter);
bjoernz
  • 3,852
  • 18
  • 30
  • this doesn't work. "No matching function for call to QPainter::QPainter(QImage&)" –  Nov 24 '10 at 18:37
  • Candidates are QPainter::QPainter(QPaintDevice*)... The constructor is expecting a pointer, so you need to give the address of the image to the constructor. – bjoernz Nov 24 '10 at 20:01
  • Is it faster to draw onto image or to overlay a qtextlabel? – user1767754 Oct 17 '14 at 22:40