相关组件的创建就不说了,效果如下
信号槽
QObject::connect(blastColor, SIGNAL(clicked()), this, SLOT(changeColor()));
槽函数
void Particlewindow::changeColor() {
QColor color = QColorDialog::getColor(Qt::red, this, QStringLiteral("选择你要的颜色"), QColorDialog::ShowAlphaChannel);
int cred = QString::number(color.red(), 10).toInt();
int cgreen = QString::number(color.green(), 10).toInt();
int cblue = QString::number(color.blue(), 10).toInt();
int calpha = QString::number(color.alpha(), 10).toInt();
QPushButton *pushButton = qobject_cast<QPushButton *>(sender());
pushButton->setIcon(createColorIcon(QColor(cred, cgreen, cblue, calpha)));
}
绘制函数
QIcon Particlewindow::createColorIcon(QColor color)
{
QPixmap pixmap(16, 16);
QPainter painter(&pixmap);
painter.setPen(Qt::NoPen);
painter.fillRect(QRect(0, 0, 16, 16), color);
return QIcon(pixmap);
}