如何从 QLabel 中裁剪图像以进行舍入?
How do I get a crop an image in a QLabel from it to be rounded?
我已经尝试使用 QLabel{border-radius : 5px;}
将其包含在样式表中,但我一直得到相同的结果 Result
这是我编写的用于生成 window 的代码:
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
和 paintEvent()
中的 painter.setClipPath(const QPainterPath&)
轻松实施。
////////////////////////// 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, 0, radius, radius, 90, -90);
clipPath.arcTo(r.right() - radius, r.bottom() - radius, radius, radius, 0, -90);
clipPath.arcTo(r.left(), r.bottom() - radius, radius, radius, 270, -90);
clipPath.arcTo(r.left(), r.top(), radius, radius, 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, 0, 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();
}
我已经尝试使用 QLabel{border-radius : 5px;}
将其包含在样式表中,但我一直得到相同的结果 Result
这是我编写的用于生成 window 的代码:
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
和 paintEvent()
中的 painter.setClipPath(const QPainterPath&)
轻松实施。
////////////////////////// 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, 0, radius, radius, 90, -90);
clipPath.arcTo(r.right() - radius, r.bottom() - radius, radius, radius, 0, -90);
clipPath.arcTo(r.left(), r.bottom() - radius, radius, radius, 270, -90);
clipPath.arcTo(r.left(), r.top(), radius, radius, 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, 0, 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();
}