ZBLOG

如何使用 QLabel 显示文本和图像?

使用QLabel显示文本和图像的步骤如下:

  1. 创建一个QLabel对象,例如label。

  2. 要在标签中显示文本,请使用setText()方法。例如,label->setText(“Hello World”);

  3. 要在标签中显示图像,请使用setPixmap()方法,并将QPixmap对象作为参数传递。例如,QPixmap pixmap(“image.png”); label->setPixmap(pixmap);

完整示例代码如下:

#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtGui/QPixmap>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // Create a QLabel object for displaying text
    QLabel *textLabel = new QLabel("Hello World");

    // Create a QLabel object for displaying an image
    QPixmap pixmap("image.png");
    QLabel *imageLabel = new QLabel;
    imageLabel->setPixmap(pixmap);

    // Show the labels
    textLabel->show();
    imageLabel->show();

    return app.exec();
}

注意:该示例假设您有一个名为“image.png”的PNG格式图像文件,并且该文件与应用程序可执行文件位于同一目录中。如果您的情况不同,请相应更改pixmap对象的构造函数参数。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=6882

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?