提交 bcf509b5 编写于 作者: 之一Yo's avatar 之一Yo

添加头像组件

上级 5f969def
# coding:utf-8
import os
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QMovie
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout
from qfluentwidgets import AvatarWidget
class Demo(QWidget):
def __init__(self):
super().__init__()
self.resize(400, 300)
self.setStyleSheet('Demo {background: white}')
self.hBoxLayout = QHBoxLayout(self)
avatar = QPixmap('resource/shoko.png')
# avatar = 'resource/boqi.gif'
sizes = [96, 48, 32, 24]
for s in sizes:
w = AvatarWidget(avatar, self)
w.setRadius(s / 2)
self.hBoxLayout.addWidget(w)
if __name__ == '__main__':
# enable dpi scale
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
app = QApplication(sys.argv)
w = Demo()
w.show()
app.exec_()
......@@ -3,7 +3,7 @@ from PyQt5.QtCore import Qt
from PyQt5.QtDesigner import QPyDesignerCustomWidgetPlugin
from qfluentwidgets import (BodyLabel, CaptionLabel, StrongBodyLabel, SubtitleLabel, TitleLabel, LargeTitleLabel,
DisplayLabel, ImageLabel)
DisplayLabel, ImageLabel, AvatarWidget)
from plugin_base import PluginBase
......@@ -128,3 +128,19 @@ class ImageLabelPlugin(LabelPlugin, QPyDesignerCustomWidgetPlugin):
def domXml(self):
return f"""<widget class="{self.name()}" name="{self.name()}"></widget>"""
class AvatarPlugin(LabelPlugin, QPyDesignerCustomWidgetPlugin):
""" Avatar plugin """
def createWidget(self, parent):
return AvatarWidget(self.icon().pixmap(72, 72), parent)
def icon(self):
return super().icon("PersonPicture")
def name(self):
return "AvatarWidget"
def domXml(self):
return f"""<widget class="{self.name()}" name="{self.name()}"></widget>"""
......@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: GPLv3, see LICENSE for more details.
"""
__version__ = "1.1.2"
__version__ = "1.1.3"
from .components import *
from .common import *
......
......@@ -12,7 +12,7 @@ from .command_bar import CommandBar, CommandButton, CommandBarView
from .line_edit import LineEdit, TextEdit, PlainTextEdit, LineEditButton, SearchLineEdit
from .icon_widget import IconWidget
from .label import (PixmapLabel, CaptionLabel, StrongBodyLabel, BodyLabel, SubtitleLabel, TitleLabel,
LargeTitleLabel, DisplayLabel, FluentLabelBase, ImageLabel)
LargeTitleLabel, DisplayLabel, FluentLabelBase, ImageLabel, AvatarWidget)
from .list_view import ListWidget, ListView, ListItemDelegate
from .menu import (DWMMenu, LineEditMenu, RoundMenu, MenuAnimationManager, MenuAnimationType, IndicatorMenuItemDelegate,
MenuItemDelegate, ShortcutMenuItemDelegate, CheckableMenu, MenuIndicatorType, SystemTrayMenu,
......
# coding:utf-8
from typing import List, Union
from PyQt5.QtCore import Qt, pyqtProperty, QPoint, pyqtSignal, QSize
from PyQt5.QtCore import Qt, pyqtProperty, QPoint, pyqtSignal, QSize, QRectF
from PyQt5.QtGui import (QPixmap, QPainter, QPalette, QColor, QFont, QImage, QPainterPath,
QImageReader, QBrush, QMovie)
from PyQt5.QtWidgets import QLabel, QWidget, QApplication
......@@ -183,21 +183,28 @@ class ImageLabel(QLabel):
super().__init__(parent)
self.image = QImage()
self.setBorderRadius(0, 0, 0, 0)
self._postInit()
@__init__.register
def _(self, image: str, parent=None):
self.__init__(parent)
self.setImage(image)
self._postInit()
@__init__.register
def _(self, image: QImage, parent=None):
self.__init__(parent)
self.setImage(image)
self._postInit()
@__init__.register
def _(self, image: QPixmap, parent=None):
self.__init__(parent)
self.setImage(image)
self._postInit()
def _postInit(self):
pass
def _onFrameChanged(self, index: int):
self.image = self.movie().currentImage()
......@@ -343,4 +350,46 @@ class ImageLabel(QLabel):
@bottomRightRadius.setter
def bottomRightRadius(self, radius: int):
self.setBorderRadius(self.topLeftRadius, self.topRightRadius, self.bottomLeftRadius, radius)
self.setBorderRadius(
self.topLeftRadius, self.topRightRadius, self.bottomLeftRadius, radius)
class AvatarWidget(ImageLabel):
""" Avatar widget """
def _postInit(self):
self.setRadius(48)
def getRadius(self):
return self._radius
def setRadius(self, radius: int):
self._radius = radius
self.setFixedSize(2*radius, 2*radius)
self.update()
def paintEvent(self, e):
if self.isNull():
return
painter = QPainter(self)
painter.setRenderHints(QPainter.Antialiasing)
# center crop image
image = self.image.scaled(
self.size()*self.devicePixelRatioF(), Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation) # type: QImage
iw, ih = image.width(), image.height()
d = self.getRadius() * 2 * self.devicePixelRatioF()
x, y = (iw - d) / 2, (ih - d) / 2
image = image.copy(x, y, d, d)
# draw image
path = QPainterPath()
path.addEllipse(QRectF(self.rect()))
painter.setPen(Qt.NoPen)
painter.setClipPath(path)
painter.drawImage(self.rect(), image)
radius = pyqtProperty(int, getRadius, setRadius)
\ No newline at end of file
......@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup(
name="PyQt-Fluent-Widgets",
version="1.1.2",
version="1.1.3",
keywords="pyqt fluent widgets",
author="zhiyiYo",
author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册