源码实现:
QString foreground = TOOLBOX.qColor2qString(forecolor); QString background = TOOLBOX.qColor2qString(backcolor); //eg."QLabel{color:rgb(255,0,0);background-color:rgb(200,101,102);}" QString qss = QString("QLabel{color:%1;background-color:%2;font:13pt}").arg(foreground).arg(background); ui->label_systemstatus->setStyleSheet(qss);
转换方法:
QString CToolBox::qColor2qString(const QColor &color, const QString &type) { if (type == "RGBA") { return QString("rgba(%1,%2,%3,%4)") .arg(color.red()) .arg(color.green()) .arg(color.blue()) .arg(color.alpha()); } else if (type == "RGB") { return QString("rgb(%1,%2,%3)") .arg(color.red()) .arg(color.green()) .arg(color.blue()); } else if (type == "HEX") { return QString().sprintf("#%1%02X%02X%02X", color.red(), color.green(), color.blue()) .arg(color.alpha() != 255 ? QString().sprintf("%02X", color.alpha()) : QString()); } return color.name(); }