在使用 QLabel
的样式表来设置背景图片时,可以通过指定 background-repeat
属性来控制背景图像的重复方式。如果你希望图片不重复填充,可以将其设置为 no-repeat
。以下是修改后的代码示例:
QString currentPath = QCoreApplication::applicationDirPath();
QString imagePath = currentPath + "/img/class.png";
ui->label_10->setStyleSheet(QString(
"QLabel { "
"background-image: url(%1); "
"background-color: rgb(231, 231, 231); "
"border-radius: 5px; "
"background-repeat: no-repeat; " // 添加此行
"}"
).arg(imagePath));
// ui->label_10->setFixedSize(48, 48);
ui->label_10->setToolTip("班级");
ui->label_10->installEventFilter(this);
这样,背景图像将不会重复填充,如果需要调整位置,可以考虑添加 background-position
属性,例如:
"background-position: center;"
如果希望背景图像在标签中居中显示,可以添加这行代码,如下所示:
"background-image: url(%1); background-color: rgb(231, 231, 231); border-radius: 5px; background-repeat: no-repeat; background-position: center;"
这样就能确保图片不重复,并且可以根据需要调整显示位置。
内容由零声教学AI助手提供,问题来源于学员提问