如何从中裁剪 QLabel 中的图像以进行舍入?
Posted
技术标签:
【中文标题】如何从中裁剪 QLabel 中的图像以进行舍入?【英文标题】:How do I get a crop an image in a QLabel from it to be rounded? 【发布时间】:2021-08-02 19:53:27 【问题描述】:我已经尝试使用 QLabelborder-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;");
我如何修改它以使图像变圆?提前感谢任何试图帮助我的人:-)
【问题讨论】:
【参考方案1】:可以在paintEvent()
中使用painter.setClipPath(const QPainterPath&)
轻松实现为自定义QWidget
。
////////////////////////// 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();
source
【讨论】:
以上是关于如何从中裁剪 QLabel 中的图像以进行舍入?的主要内容,如果未能解决你的问题,请参考以下文章