foreach (const QString &colorName, colorNames) { const QColor color(colorName); //样式一 // ui->comboBox->addItem(tr(colorName.toStdString().c_str()), color); // const QModelIndex idx = ui->comboBox->model()->index(index++, 0); // ui->comboBox->model()->setData(idx, color, Qt::BackgroundColorRole); //样式二 QImage image(12,12,QImage::Format_RGB32); QPainter painter(&image); painter.fillRect(0,0,12,12,color); QIcon icon; icon.addPixmap(QPixmap::fromImage(image)); ui->comboBox->addItem(icon, tr(colorName.toStdString().c_str())); }