开发者

Qt - How to set text on top of QLabel Image

开发者 https://www.devze.com 2023-01-26 15:03 出处:网络
I believe QPainter is used, but I can\'t figure out how to combine the two. QLabel* imageLabel = new QLabel();

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("Ari开发者_如何学Pythonal", 30));
painter->drawText(rect(), Qt::AlignCenter, "Text on Image");


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消