MCPcopy
hub / github.com/PyQt5/PyQt / NotificationItem

Class NotificationItem

Demo/Notification.py:58–154  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56
57
58class NotificationItem(QWidget):
59 closed = pyqtSignal(QListWidgetItem)
60
61 def __init__(self, title, message, item, *args, ntype=0, callback=None, **kwargs):
62 super(NotificationItem, self).__init__(*args, **kwargs)
63 self.item = item
64 self.callback = callback
65 layout = QHBoxLayout(self, spacing=0)
66 layout.setContentsMargins(0, 0, 0, 0)
67 self.bgWidget = QWidget(self) # 背景控件, 用于支持动画效果
68 layout.addWidget(self.bgWidget)
69
70 layout = QGridLayout(self.bgWidget)
71 layout.setHorizontalSpacing(15)
72 layout.setVerticalSpacing(10)
73
74 # 标题左边图标
75 layout.addWidget(
76 QLabel(self, pixmap=NotificationIcon.icon(ntype)), 0, 0)
77
78 # 标题
79 self.labelTitle = QLabel(title, self)
80 font = self.labelTitle.font()
81 font.setBold(True)
82 font.setPixelSize(22)
83 self.labelTitle.setFont(font)
84
85 # 关闭按钮
86 self.labelClose = QLabel(
87 self, cursor=Qt.PointingHandCursor, pixmap=NotificationIcon.icon(NotificationIcon.Close))
88
89 # 消息内容
90 self.labelMessage = QLabel(
91 message, self, cursor=Qt.PointingHandCursor, wordWrap=True, alignment=Qt.AlignLeft | Qt.AlignTop)
92 font = self.labelMessage.font()
93 font.setPixelSize(20)
94 self.labelMessage.setFont(font)
95 self.labelMessage.adjustSize()
96
97 # 添加到布局
98 layout.addWidget(self.labelTitle, 0, 1)
99 layout.addItem(QSpacerItem(
100 40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum), 0, 2)
101 layout.addWidget(self.labelClose, 0, 3)
102 layout.addWidget(self.labelMessage, 1, 1, 1, 2)
103
104 # 边框阴影
105 effect = QGraphicsDropShadowEffect(self)
106 effect.setBlurRadius(12)
107 effect.setColor(QColor(0, 0, 0, 25))
108 effect.setOffset(0, 2)
109 self.setGraphicsEffect(effect)
110
111 self.adjustSize()
112
113 # 5秒自动关闭
114 self._timer = QTimer(self, timeout=self.doClose)
115 self._timer.setSingleShot(True) # 只触发一次

Callers 4

infoMethod · 0.85
successMethod · 0.85
warningMethod · 0.85
errorMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected