1.思路:标签QLabel 改变形状,根据情况显示背景色
2.代码实现
setLED(QLabel *label, int color, int size)
{
label->setText("");
QString min_width = QString("min-width: %1px;").arg(size);
QString min_height = QString("min-height: %1px;").arg(size);
QString max_width = QString("max-width: %1px;").arg(size);
QString max_height = QString("max-height: %1px;").arg(size);
QString border_radius = QString("border-radius: %1px;").arg(size/2);
QString border = QString("border:1px solid black;");
QString background = "background-color:";
switch (color) {
case 0:
//灰色
background += "rgb(190, 190, 190)";
break;
case 1:
//红色
background += "rgb(255, 0, 0)";
break;
case 2:
//绿色
background += "rgb(0, 255, 0)";
break;
default:
break;
}
const QString Sheetstyle1 = min_width+min_height+max_width+max_height +border_radius+border+background;
label->setStyleSheet(Sheetstyle1);
}
3.结果显示