你可以将这段代码合并成一个更紧凑的形式。具体来说,可以将设置样式表的两部分合并到一起,这样就只需要调用一次 setStyleSheet
方法。以下是合并后的代码示例:
QString currentPath = QCoreApplication::applicationDirPath();
QString pathnew = currentPath + "/img/rollcall.svg";
QLabel* label = (QLabel*)object;
label->setStyleSheet(QString("border-radius: 10px;"
"background-color: rgb(235, 235, 235);"
"background-image: url(%1);"
"background-repeat: no-repeat;"
"background-position: center;").arg(pathnew));
在这个合并版本中,我们首先计算出 pathnew
,然后将其与其他样式表属性组合在一起,并通过单一的 setStyleSheet
调用来设置标签的样式。这种方式使代码更加简洁和易于维护。
内容由零声教学AI助手提供,问题来源于学员提问