diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index d8756b6..d49de26 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -19,6 +19,7 @@ encoding//\u5B57\u4F53\u6D4B\u8BD5/TestFontRoboto.py=utf-8 encoding//\u68A6\u5E7B\u6811/DreamTree.py=utf-8 encoding//\u6D4F\u89C8\u5668\u83B7\u53D6Cookie/WebEngineView.py=utf-8 encoding//\u6D4F\u89C8\u5668\u83B7\u53D6Cookie/WebView.py=utf-8 +encoding//\u754C\u9762\u7F8E\u5316/QLabel\u5706\u5F62\u5934\u50CF/CircleLabel.py=utf-8 encoding//\u754C\u9762\u7F8E\u5316/QMessageBox\u6837\u5F0F/\u65B9\u6848\u4E00/critical.py=utf-8 encoding//\u754C\u9762\u7F8E\u5316/QMessageBox\u6837\u5F0F/\u65B9\u6848\u4E00/information.py=utf-8 encoding//\u754C\u9762\u7F8E\u5316/QMessageBox\u6837\u5F0F/\u65B9\u6848\u4E00/question.py=utf-8 diff --git a/README.md b/README.md index de6bd87..13ec261 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,5 @@ ### [4.界面美化](界面美化/) - [4.1 QMessageBox样式](界面美化/QMessageBox样式) - - [4.2 QScrollBar滚动条样式](界面美化/QScrollBar滚动条样式) \ No newline at end of file + - [4.2 QScrollBar滚动条样式](界面美化/QScrollBar滚动条样式) + - [4.3 QLabel圆形头像](界面美化/QLabel圆形头像) \ No newline at end of file diff --git a/界面美化/QLabel圆形头像/CircleLabel.py b/界面美化/QLabel圆形头像/CircleLabel.py new file mode 100644 index 0000000..86372d4 --- /dev/null +++ b/界面美化/QLabel圆形头像/CircleLabel.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +''' +Created on 2018年1月20日 +@author: Irony."[讽刺] +@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447 +@email: 892768447@qq.com +@file: CircleLabel +@description: +''' +from PyQt5.QtCore import Qt +from PyQt5.QtGui import QPixmap, QPainter, QPainterPath +from PyQt5.QtWidgets import QLabel, QWidget, QHBoxLayout + + +__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com" +__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]" +__Version__ = "Version 1.0" + + +class Label(QLabel): + + def __init__(self, *args, antialiasing=True, **kwargs): + super(Label, self).__init__(*args, **kwargs) + self.Antialiasing = antialiasing + self.setMaximumSize(200, 200) + self.setMinimumSize(200, 200) + self.radius = 100 + + #####################核心实现######################### + self.target = QPixmap(self.size()) # 大小和控件一样 + self.target.fill(Qt.transparent) # 填充背景为透明 + + p = QPixmap("head.jpg").scaled( # 加载图片并缩放和控件一样大 + 200, 200, 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) + +# painter.setPen(# 测试圆圈 +# QPen(Qt.red, 5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin)) + path = QPainterPath() + path.addRoundedRect( + 0, 0, self.width(), self.height(), self.radius, self.radius) + #**** 切割为圆形 ****# + painter.setClipPath(path) +# painter.drawPath(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: black;") + + +if __name__ == "__main__": + import sys + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = Window() + w.show() + sys.exit(app.exec_()) diff --git a/界面美化/QLabel圆形头像/README.md b/界面美化/QLabel圆形头像/README.md new file mode 100644 index 0000000..e24ec51 --- /dev/null +++ b/界面美化/QLabel圆形头像/README.md @@ -0,0 +1,10 @@ +# QLabel圆形头像 + +一种比较好的方法制作圆形头像 + +### 简单说明 +该方法使用QPainter的 setClipPath 方法结合QPainterPath对图片进行裁剪。 + +截图 + +![1](ScreenShot/1.png) \ No newline at end of file diff --git a/界面美化/QLabel圆形头像/ScreenShot/1.png b/界面美化/QLabel圆形头像/ScreenShot/1.png new file mode 100644 index 0000000..818d182 Binary files /dev/null and b/界面美化/QLabel圆形头像/ScreenShot/1.png differ diff --git a/界面美化/QLabel圆形头像/head.jpg b/界面美化/QLabel圆形头像/head.jpg new file mode 100644 index 0000000..74efcdc Binary files /dev/null and b/界面美化/QLabel圆形头像/head.jpg differ