两种方法:
1.通过QPalette设置:
//颜色对话框获取颜色
QColor clr = QColorDialog::getColor(Qt::red,this,tr("颜色对话框"));
//获取Label调色板
QPalette palette = ui->labColor->palette();
//修改背景色
palette.setColor( QPalette::Background, clr);
ui->labColor->setAutoFillBackground(true);
//重新加载调色板
ui->labColor->setPalette(palette);
2.通过setStyleSheet方法修改背景色:
//颜色对话框获取颜色
QColor clr = QColorDialog::getColor(Qt::red,this,tr("颜色对话框"));
//格式化style sheet,背景色设置
QString str = QString("QLabel{background:%1;}").arg(clr.name(QColor::HexRgb));
//setStyleSheet
ui->labColor->setStyleSheet(str);