Let’s not say much, look at the picture first:
**The first method: **Use Qpainter to crop the picture, there is nothing wrong with it, it is recommended.
**Second method: **Use the bitmap setMask() to set the mask (look carefully at the picture there are jagged, unresolved, you can not use the bitmap, try to use the picture as a mask)
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsDropShadowEffect>
#include <QPropertyAnimation>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
Q_PROPERTY(int borderRadius READ getBorderRadius WRITE setBorderRadius)
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
void init();
protected:
QPixmap scaledPixmap(const QPixmap &src,int width,int height); // 压缩图片为指定宽高
QPixmap generatePixmap(const QPixmap &src,const int &radius); // 实现圆角图片
QPixmap pixmapToRound(const QPixmap &src,const int &radius); // mask实现圆角图片
int getBorderRadius() const;
void setBorderRadius(const int radius);
private:
QGraphicsDropShadowEffect *m_labelDrawShadowEffect; // labelDraw边框阴影
QGraphicsDropShadowEffect *m_labelMaskShadowEffect; // labelMask边框阴影
QPropertyAnimation *m_labelDrawAnimation;
QPropertyAnimation *m_labelMaskAnimation;
int m_borderRadius;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QBitmap>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
this->setStyleSheet("QGroupBox {"
" background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
" stop: 0 #E0E0E0, stop: 1 #FFFFFF);"
" border: 1px solid red;"
" border-radius: 5px;"
" }"
);
QPixmap pixmap(":/1.jpg");
ui->label_draw->setPixmap(generatePixmap(pixmap,ui->label_draw->width()));
ui->label_mask->setPixmap(pixmapToRound(pixmap,ui->label_mask->width()));
// 设置边框阴影
m_labelDrawShadowEffect=new QGraphicsDropShadowEffect();
m_labelDrawShadowEffect->setColor(Qt::darkGreen);
m_labelDrawShadowEffect->setOffset(0,0);
m_labelDrawShadowEffect->setBlurRadius(0);
m_labelMaskShadowEffect=new QGraphicsDropShadowEffect();
m_labelMaskShadowEffect->setColor(Qt::red);
m_labelMaskShadowEffect->setOffset(0,0);
m_labelMaskShadowEffect->setBlurRadius(0);
ui->label_draw->setGraphicsEffect(m_labelDrawShadowEffect);
ui->label_mask->setGraphicsEffect(m_labelMaskShadowEffect);
// 动画效果
m_labelDrawAnimation=new QPropertyAnimation();
m_labelDrawAnimation->setTargetObject(this);
m_labelDrawAnimation->setDuration(2000);
m_labelDrawAnimation->setPropertyName("borderRadius");
m_labelDrawAnimation->setKeyValueAt(0,1);
m_labelDrawAnimation->setKeyValueAt(0.5,25);
m_labelDrawAnimation->setKeyValueAt(1,1);
m_labelDrawAnimation->setLoopCount(-1);
m_labelDrawAnimation->start();
m_labelMaskAnimation=new QPropertyAnimation();
m_labelMaskAnimation->setTargetObject(this);
m_labelMaskAnimation->setDuration(2000);
m_labelMaskAnimation->setPropertyName("borderRadius");
m_labelMaskAnimation->setKeyValueAt(0,1);
m_labelMaskAnimation->setKeyValueAt(0.5,25);
m_labelMaskAnimation->setKeyValueAt(1,1);
m_labelMaskAnimation->setLoopCount(-1);
m_labelMaskAnimation->start();
}
QPixmap MainWindow::scaledPixmap(const QPixmap &src, int width, int height)
{
return src.scaled(width,(height==0?width:height),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
}
QPixmap MainWindow::generatePixmap(const QPixmap &src, const int &radius)
{
if(src.isNull())
{
return src;
}
// 压缩图片为指定大小
QPixmap pixmap=scaledPixmap(src,radius*2,radius*2);
QPixmap dest(2*radius,2*radius);
dest.fill(Qt::transparent);
QPainter painter(&dest);
// 抗锯齿/平滑边缘处理
painter.setRenderHints(QPainter::Antialiasing,true);
painter.setRenderHints(QPainter::SmoothPixmapTransform,true);
// 裁剪为圆角
QPainterPath path;
path.addEllipse(0,0,2*radius,2*radius);
painter.setClipPath(path);
painter.drawPixmap(0,0,2*radius,2*radius,pixmap);
return dest;
}
QPixmap MainWindow::pixmapToRound(const QPixmap &src, const int &radius)
{
if(src.isNull())
{
return src;
}
QSize size(2*radius, 2*radius);
QBitmap mask(size);
QPainter painter(&mask);
painter.setPen(Qt::NoPen);
// 抗锯齿/平滑边缘处理
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
// 填充矩形区域颜色
painter.fillRect(mask.rect(), Qt::white);
painter.setBrush(QColor(0, 0, 0));
//painter.drawEllipse(0,0,radius*2,radius*2);
painter.drawRoundedRect(mask.rect(), radius, radius);
// 压缩图片为指定大小
QPixmap pixmap=scaledPixmap(src,radius*2,radius*2);
pixmap.setMask(mask);
return pixmap;
}
int MainWindow::getBorderRadius() const
{
return m_borderRadius;
}
void MainWindow::setBorderRadius(const int radius)
{
m_borderRadius=radius;
m_labelDrawShadowEffect->setBlurRadius(m_borderRadius);
m_labelMaskShadowEffect->setBlurRadius(m_borderRadius);
}