paintEvent 加载图片
QPainter painter(this);
QImage m_buttonIcon("://images/EducationCenter/playlist_left.svg");
QColor color;
if (m_isMouseEnter) {
color.setRgb(0, 128, 255, 1);
} else {
color.setRgb(255, 255, 255, 1);
}
//函数调用
setCenterImageColor(m_buttonIcon, color);
painter.drawImage(QRect((width() - 5) / 2, (height() - 9) / 2, 5, 9), m_buttonIcon);
修改svg颜色方法
void PlayListContorButton::setCenterImageColor(QImage &image, QColor &color)
{
image = image.convertToFormat(QImage::Format_RGBA8888_Premultiplied, Qt::NoFormatConversion);
int bmpWidth = image.width();
int bmpHeight = image.height();
for (int i = 0; i < bmpWidth; ++i) {
for (int j = 0; j < bmpHeight; ++j) {
//将灰色(0~255)全部替换成设定的颜色,全透明的不替换
QColor tempColor = image.pixelColor(i, j);
if (tempColor.alpha() != 0) {
color.setAlpha(tempColor.alpha());
image.setPixelColor(i, j, color);
}
}
}
}
参考: https://blog.csdn.net/octdream/article/details/79492778