Qt label 设置背景图并写文字图片在下文字在上
时间: 2024-09-26 14:02:40 浏览: 66
在Qt中,你可以使用`QLabel`和一些CSS样式来设置背景图像并在其上显示文本。首先,你需要创建一个`QLabel`实例,然后设置其背景图像:
```cpp
#include <QApplication>
#include <QWidget>
#include <QLabel>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QWidget window;
window.setWindowTitle("QLabel with Background Image");
QLabel *label = new QLabel(&window);
label->setStyleSheet(
"background-image: url('path_to_your_image.jpg'); /* 替换为你的图片路径 */\n"
"color: white; /* 文字颜色,可以根据需要调整 */\n"
"text-align: center; /* 文字居中 */\n"
"vertical-align: bottom; /* 图片在上,文字在下 */");
label->setText("这是你的文字"); // 文本内容
window.setFixedSize(400, 200); // 窗口大小
window.show();
return app.exec();
}
```
在这个例子中,你需要将`'path_to_your_image.jpg'`替换为你要使用的图片的实际路径。`vertical-align: bottom;`这一行使得文字位于图片下方。
阅读全文
相关推荐
















