如何将通知编号添加到按钮图标? [英] How to add notification number to a button icon?

查看:77
本文介绍了如何将通知编号添加到按钮图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a GUI with PyQt5. It will have a notification button with an icon. I want to add a small bubble with the number of notifications on the icon.

如果一个数字是不可能的,我想用一个红点作为备用方法.但是我应该如何跟踪新通知(例如通知的侦听器)并在窗口运行时更改图标?

If a number is not possible, I would like to use a red dot as a backup method. But how should I keep track of the new notifications (like a listener for notification) and change the icon while the window is running?

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

I have been googling about this problem, but only mobile development stuff and non-PyQt5 related results come up.

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

Expected result: Let's say we have a list. And the icon of the button will automatically change when a new item is added to the list. Then when the button is clicked, the icon will change back.

推荐答案

一个可能的解决方案是创建一个小部件,其布局中放置一个 QToolButton,并在右上角放置一个 QLabel 和一个带有编号的 QPixmap

A possible solution is to create a widget that has a layout where you place a QToolButton and at the top right a QLabel with a QPixmap that has the number

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_())

这篇关于如何将通知编号添加到按钮图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆