使用QLabel显示文本和图像的步骤如下:
创建一个QLabel对象,例如label。
要在标签中显示文本,请使用setText()方法。例如,label->setText(“Hello World”);
要在标签中显示图像,请使用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对象的构造函数参数。