【问题标题】:Qt - How to set text on top of QLabel ImageQt - 如何在 QLabel Image 上设置文本
【发布时间】:2010-11-24 18:25:58
【问题描述】:

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

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

【问题讨论】:

    标签: qt mobile qt4 nokia


    【解决方案1】:

    你需要告诉画家在哪里画:

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

    【讨论】:

    • 这不起作用。 "没有匹配的函数调用 QPainter::QPainter(QImage&)"
    • 候选人是QPainter::QPainter(QPaintDevice*)...构造函数需要一个指针,所以你需要将图像的地址给构造函数。
    • 在图像上绘制或覆盖 qtextlabel 是否更快?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2021-10-20
    • 2023-03-07
    • 2018-05-15
    • 2020-09-13
    相关资源
    最近更新 更多