Qt-如何在QLabel图像上方设置文本 [英] Qt - How to set text on top of QLabel Image

查看:1378
本文介绍了Qt-如何在QLabel图像上方设置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信使用了QPainter,但是我不知道如何将两者结合起来.

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

推荐答案

您需要告诉画家绘制位置:

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

这篇关于Qt-如何在QLabel图像上方设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆