gpt4 book ai didi

python - 如何将通知编号添加到按钮图标?

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:25 26 4
gpt4 key购买 nike

我正在尝试使用 PyQt5 制作一个 GUI。它将有一个带有图标的通知按钮。我想在图标上添加一个带有通知数量的小气泡。

如果无法使用数字,我想使用红点作为备用方法。但是我应该如何跟踪新通知(如通知的监听器)并在窗口运行时更改图标?

我一直在谷歌搜索这个问题,但只有移动开发的东西和非 PyQt5 相关的结果出现。

预期结果:假设我们有一个列表。当一个新项目被添加到列表时,按钮的图标会自动改变。然后当点击按钮时,图标将变回。

最佳答案

一个可能的解决方案是创建一个具有布局的小部件,您可以在其中放置一个 QToolButton 并在右上角放置一个带有 QPixmap 的 QLabel,该 QPixmap 具有数字

from PyQt5 import QtCore, QtGui, QtWidgets


def create_pixmap(point, radius=64):
rect = QtCore.QRect(QtCore.QPoint(), 2 * radius * QtCore.QSize(1, 1))
pixmap = QtGui.QPixmap(rect.size())
rect.adjust(1, 1, -1, -1)
pixmap.fill(QtCore.Qt.transparent)
painter = QtGui.QPainter(pixmap)
painter.setRenderHints(
QtGui.QPainter.Antialiasing | QtGui.QPainter.TextAntialiasing
)
pen = painter.pen()
painter.setPen(QtCore.Qt.NoPen)

gradient = QtGui.QLinearGradient()
gradient.setColorAt(1, QtGui.QColor("#FD6684"))
gradient.setColorAt(0, QtGui.QColor("#E0253F"))
gradient.setStart(0, rect.height())
gradient.setFinalStop(0, 0)

painter.setBrush(QtGui.QBrush(gradient))
painter.drawEllipse(rect)
painter.setPen(pen)
painter.drawText(rect, QtCore.Qt.AlignCenter, str(point))
painter.end()
return pixmap


class NotificationButton(QtWidgets.QWidget):
scoreChanged = QtCore.pyqtSignal(int)

def __init__(self, score=0, icon=QtGui.QIcon(), radius=12, parent=None):
super(NotificationButton, self).__init__(parent)

self.m_score = score
self.m_radius = radius

self.setContentsMargins(0, self.m_radius, self.m_radius, 0)
self.m_button = QtWidgets.QToolButton(clicked=self.clear)
self.m_button.setContentsMargins(0, 0, 0, 0)
self.m_button.setIcon(icon)
self.m_button.setIconSize(QtCore.QSize(18, 18))
lay = QtWidgets.QVBoxLayout(self)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(self.m_button)
self.m_label = QtWidgets.QLabel(self)
self.m_label.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
self.m_label.raise_()
self.setSizePolicy(self.m_button.sizePolicy())
self.update_notification()

@QtCore.pyqtProperty(int, notify=scoreChanged)
def score(self):
return self.m_score

@score.setter
def score(self, score):
if self.m_score != score:
self.m_score = score
self.update_notification()
self.scoreChanged.emit(score)

@QtCore.pyqtSlot()
def clear(self):
self.score = 0

@QtCore.pyqtProperty(int)
def radius(self):
return self.m_radius

@radius.setter
def radius(self, radius):
self.m_radius = radius
self.update_notification()

def update_notification(self):
self.setContentsMargins(0, self.m_radius, self.m_radius, 0)
self.m_label.setPixmap(create_pixmap(self.m_score, self.m_radius))
self.m_label.adjustSize()

def resizeEvent(self, event):
self.m_label.move(self.width() - self.m_label.width(), 0)
super(NotificationButton, self).resizeEvent(event)


class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)

self.m_item_le = QtWidgets.QLineEdit("Stack Overflow")
add_button = QtWidgets.QPushButton("Add", clicked=self.add_item)
self.m_notification_button = NotificationButton(
icon=QtGui.QIcon("image.png")
)
self.m_list_widget = QtWidgets.QListWidget()

vlay = QtWidgets.QVBoxLayout(self)
hlay = QtWidgets.QHBoxLayout()
hlay.addWidget(self.m_item_le)
hlay.addWidget(add_button)
vlay.addLayout(hlay)
vlay.addWidget(
self.m_notification_button, alignment=QtCore.Qt.AlignRight
)
vlay.addWidget(self.m_list_widget)

@QtCore.pyqtSlot()
def add_item(self):
text = self.m_item_le.text()
self.m_list_widget.addItem(
"%s: %s" % (self.m_list_widget.count(), text)
)
self.m_notification_button.score += 1
self.m_list_widget.scrollToBottom()


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

enter image description here

关于python - 如何将通知编号添加到按钮图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484679/

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