无边框圆角对话框

This commit is contained in:
Irony 2019-04-23 10:50:19 +08:00
parent 377affd9f2
commit 0fd4646a75
5 changed files with 99 additions and 1 deletions

View file

@ -4,6 +4,7 @@ encoding//Demo/CircleLine.py=utf-8
encoding//Demo/EmbedWindow.py=utf-8
encoding//Demo/FacePoints.py=utf-8
encoding//Demo/FollowWindow.py=utf-8
encoding//Demo/FramelessDialog.py=utf-8
encoding//Demo/FramelessWindow.py=utf-8
encoding//Demo/Lib/Application.py=utf-8
encoding//Demo/Lib/FramelessWindow.py=utf-8

85
Demo/FramelessDialog.py Normal file
View file

@ -0,0 +1,85 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年4月19日
@author: Irony
@site: https://pyqt5.com https://github.com/892768447
@email: 892768447@qq.com
@file: FramelessDialog
@description: 无边框圆角对话框
"""
from PyQt5.QtCore import Qt, QSize, QTimer
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QWidget,\
QGraphicsDropShadowEffect, QPushButton, QGridLayout, QSpacerItem,\
QSizePolicy
__Author__ = "Irony"
__Copyright__ = 'Copyright (c) 2019 Irony'
__Version__ = 1.0
Stylesheet = """
#Custom_Widget {
background: white;
border-radius: 10px;
}
#closeButton {
min-width: 36px;
min-height: 36px;
font-family: "Webdings";
qproperty-text: "r";
border-radius: 10px;
}
#closeButton:hover {
color: white;
background: red;
}
"""
class Dialog(QDialog):
def __init__(self, *args, **kwargs):
super(Dialog, self).__init__(*args, **kwargs)
self.setObjectName('Custom_Dialog')
self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setStyleSheet(Stylesheet)
self.initUi()
# 添加阴影
effect = QGraphicsDropShadowEffect(self)
effect.setBlurRadius(12)
effect.setOffset(0, 0)
effect.setColor(Qt.gray)
self.setGraphicsEffect(effect)
def initUi(self):
layout = QVBoxLayout(self)
# 重点: 这个widget作为背景和圆角
self.widget = QWidget(self)
self.widget.setObjectName('Custom_Widget')
layout.addWidget(self.widget)
# 在widget中添加ui
layout = QGridLayout(self.widget)
layout.addItem(QSpacerItem(
40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum), 0, 0)
layout.addWidget(QPushButton(
'r', self, clicked=self.accept, objectName='closeButton'), 0, 1)
layout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum,
QSizePolicy.Expanding), 1, 0)
def sizeHint(self):
return QSize(600, 400)
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Dialog()
w.exec_()
QTimer.singleShot(200, app.quit)
sys.exit(app.exec_())

View file

@ -17,6 +17,7 @@
- [人脸特征点](#15、人脸特征点)
- [使用Threading](#16、使用Threading)
- [背景连线动画](#17、背景连线动画)
- [无边框圆角对话框](#18、无边框圆角对话框)
## 1、重启窗口Widget
[运行 RestartWindow.py](RestartWindow.py)
@ -170,4 +171,14 @@ PyQt 结合 Opencv 进行人脸检测;
主要参考 [背景连线动画.html](Data/背景连线动画.html)
![CircleLine](ScreenShot/CircleLine.gif)
![CircleLine](ScreenShot/CircleLine.gif)
## 18、无边框圆角对话框
[运行 FramelessDialog.py](FramelessDialog.py)
1. 通过设置 `self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)``self.setAttribute(Qt.WA_TranslucentBackground, True)` 达到无边框和背景透明
2. 在QDialog中放置一个QWidget作为背景和圆角
3. 在QWidget中放置其他内容
![FramelessDialog](ScreenShot/FramelessDialog.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

View file

@ -168,6 +168,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
- [嵌入外部窗口](Demo/EmbedWindow.py)
- [简单跟随其它窗口](Demo/FollowWindow.py)
- [简单探测窗口和放大截图](Demo/ProbeWindow.py)
- [无边框圆角对话框](Demo/FramelessDialog.py)
- [无边框自定义标题栏窗口](Demo/FramelessWindow.py)
- [右下角弹出框](Demo/WindowNotify.py)
- [程序重启](Demo/AutoRestart.py)