如何从 QLabel 中裁剪图像以使其四舍五入?

问题描述

我已经尝试将它包含在带有 QLabel{border-radius : 5px;} 的样式表中,但我一直得到相同的 Result

这是我为生成该窗口而编写的代码

userPreview::userPreview(QString username,QString image_path){

this->setFixedSize(200,80);
this->setStyleSheet("background : rgb(42,45,49); border-radius : 2px; border : 2px; border-color : (90,92,95);");

name = new QLabel(username,this);
name->move(90,30);
name->setStyleSheet("color : rgb(255,255,255); font-weight : bold; text-transform : uppercase;");

image = new QLabel(this);
image->setpixmap(Qpixmap(image_path).scaled(40,40));
image->move(20,20);
image->setStyleSheet("border-radius : 5px;");

} 我如何修改它以使图像变圆?预先感谢任何试图帮助我的人:-)

解决方法

可以轻松实现为自定义 QWidget,在 painter.setClipPath(const QPainterPath&) 中使用 paintEvent()

////////////////////////// roundedlabel.h

#ifndef ROUNDEDLABEL_H
#define ROUNDEDLABEL_H

#include <QWidget>

class RoundedLabel : public QWidget
{
    Q_OBJECT
public:
    explicit RoundedLabel(QWidget *parent = nullptr);
    void setPixmap(const QPixmap& pixmap);
    void setBorderRadius(int value);
    QSize sizeHint() const;

protected:
    void paintEvent(QPaintEvent *event);
    int mBorderRadius;
    QPixmap mPixmap;
};

#endif // ROUNDEDLABEL_H

////////////////////////// roundedlabel.cpp

#include "roundedlabel.h"

#include <QPainter>
#include <QPainterPath>

RoundedLabel::RoundedLabel(QWidget *parent) : QWidget(parent),mBorderRadius(0)
{

}

void RoundedLabel::setPixmap(const QPixmap &pixmap)
{
    mPixmap = pixmap;
    updateGeometry();
    update();
}

void RoundedLabel::setBorderRadius(int value)
{
    mBorderRadius = value;
    update();
}

void RoundedLabel::paintEvent(QPaintEvent *event)
{
    if (mPixmap.isNull()) {
        return;
    }
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    QRect r = this->rect();
    int radius = mBorderRadius;
    QPainterPath clipPath;
    clipPath.moveTo(radius,0);
    clipPath.arcTo(r.right() - radius,radius,90,-90);
    clipPath.arcTo(r.right() - radius,r.bottom() - radius,-90);
    clipPath.arcTo(r.left(),270,r.top(),180,-90);
    clipPath.closeSubpath();
    painter.setClipPath(clipPath);
    painter.drawPixmap(QPoint(0,0),mPixmap);
}

QSize RoundedLabel::sizeHint() const
{
    return mPixmap.isNull() ? QSize(40,40) : mPixmap.size();
}

////////////////////////// main.cpp

#include <QApplication>
#include <QVBoxLayout>
#include "roundedlabel.h"

int main(int argc,char *argv[])
{
    QApplication a(argc,argv);

    QString image_path = ":/images/photo.png";

    RoundedLabel* label = new RoundedLabel();
    label->setPixmap(QPixmap(image_path).scaled(40,40));
    label->setBorderRadius(5);

    QWidget widget;
    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(label,Qt::AlignCenter);
    widget.setLayout(layout);
    widget.setStyleSheet("background : rgb(42,45,49); border-radius : 2px; border : 2px; border-color : (90,92,95);");
    widget.show();
    widget.setGeometry(QRect(widget.pos(),QSize(200,100)));

    return a.exec();
}

source