QWiget设置窗口背景图,特别是ui给的图片的边边是有圆弧的,这个逼得我不得不给QWidget搞个圆弧,试了几个方法:
方法一:
在构造函数加上代码:
QPalette pal = this->palette();
pal.setBrush(QPalette::Background, QBrush(QPixmap("./skin/LoginBk.png")));
setPalette(pal);
在painevent实现:
QPainter painter(this);
painter.drawPixmap(0,0,"./skin/LoginBk.png");
图片显示很好,就是四个边边有残留,还是很看;这种适合没有圆角的图片;
方法二:
找了网上大神的代码
QPainter painter(this);
QColor m_defaultBackgroundColor = qRgb(65, 65, 65);
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRoundedRect(10, 10, this->width() - 20, this->height() - 20, 5, 5);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(QColor(m_defaultBackgroundColor.red(),
m_defaultBackgroundColor.green(),
m_defaultBackgroundColor.blue())));
QColor color(45, 45, 45, 50);
for (int i = 0; i < 5; i++)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRoundedRect(5 - i, 5 - i, this->width() - (5 - i) * 2, this->height() - (5 - i) * 2, 5, 5);
color.setAlpha(100 - qSqrt(i) * 50);
painter.setPen(color);
painter.drawPath(path);
}
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QBrush(Qt::white));
painter.setPen(Qt::transparent);
QRect rect = this->rect();
rect.setX(5);
rect.setY(5);
rect.setWidth(rect.width() - 5);
rect.setHeight(rect.height() - 5);
// rect: 绘制区域 15:圆角弧度
painter.drawRoundedRect(rect, 5, 5);
painter.drawPixmap(0,0,"./skin/LoginBk.png");
最后完美解决,如果找不到qSqrt方法,包含头文件qmath.h