gpt4 book ai didi

python - 带有圆形图像的 Qlabel

转载 作者:行者123 更新时间:2023-12-04 01:33:50 26 4
gpt4 key购买 nike

我想在 QT/PySide2 应用程序中显示圆形图像。

下面是我试过的代码。

self.statusWidget = QLabel()
img = QImage(":/image.jpg").scaled(49, 49, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
self.statusWidget.setPixmap(QPixmap.fromImage(img))
self.statusWidget.setStyleSheet("border-radius:20px")

我得到了低于输出。

enter image description here

我想要像下面这样的 Qlabel。

enter image description here

最佳答案

用户头像 QLabel

制作圆形头像的最佳方法

该方法使用 setClipPath QPainter 方法与 QPainterPath 结合使用
裁剪图像。

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter, QPainterPath
from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout, QApplication

class Label(QLabel):
def __init__(self, *args, antialiasing=True, **kwargs):
super(Label, self).__init__(*args, **kwargs)
self.Antialiasing = antialiasing
self.setMaximumSize(50, 50)
self.setMinimumSize(50, 50)
self.radius = 25

self.target = QPixmap(self.size())
self.target.fill(Qt.transparent)

p = QPixmap("E:/_Qt/img/qt-logo.png").scaled(
50, 50, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)

painter = QPainter(self.target)
if self.Antialiasing:
painter.setRenderHint(QPainter.Antialiasing, True)
painter.setRenderHint(QPainter.HighQualityAntialiasing, True)
painter.setRenderHint(QPainter.SmoothPixmapTransform, True)

path = QPainterPath()
path.addRoundedRect(
0, 0, self.width(), self.height(), self.radius, self.radius)

painter.setClipPath(path)
painter.drawPixmap(0, 0, p)
self.setPixmap(self.target)

class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(Label(self))
layout.addWidget(Label(self, antialiasing=False))
self.setStyleSheet("background: blue;")

if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

enter image description here

关于python - 带有圆形图像的 Qlabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50819033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com