PySide2 - 如何在标签上显示 "count of unread"?

PySide2 - How can I show the "count of unread" on label?

我知道当我触发一个编辑事件时,我将它添加到计数器中,但是如何在标签上显示它?

或者,这实际上是两个标签显示在同一个块中吗?

谢谢@Loïc G.,根据你提供的关键字,我在下面写了一些代码,

可能效果不错。但是如何在重绘之前擦除?

from PySide2 import QtCore,QtWidgets,QtGui
from PySide2.QtCore import QPoint
from PySide2.QtGui import QPainter,QPixmap,QImage
from PySide2.QtWidgets import QApplication,QLabel
import sys

if __name__ == "__main__":
    app = QApplication(sys.argv)
    image = QImage('Start.png')
    painter = QPainter()
    painter.begin(image)
    painter.setBrush(QtCore.Qt.yellow)
    center = QPoint(33,35)
    painter.drawEllipse(center,10,10)
    painter.drawText(30,40,'1')
    painter.end()

    label = QLabel()
    label.setPixmap(QPixmap.fromImage(image))
    label.show()

    sys.exit(app.exec_())

如评论所述,您必须使用 QPixmap 创建图像,然后使用 QPainter 绘制蓝色圆圈和文本,并将最终图像设置为按钮图标。

在下面,您将找到一个工作示例,其中包含 2 个按钮 increment/decrement "unread" 值。

每次更改此值时,都会发出 unreadCountChanged() 信号。

图像在 unreadCountChanged 插槽上创建并设置为按钮图标。

from PyQt4 import QtCore, QtGui
import sys


class MyApplication(QtGui.QMainWindow):
    def __init__(self):
        super(MyApplication, self).__init__()
        self.unreadCount = 0

        self.setupUi()
        self.connect(self, QtCore.SIGNAL("unreadCountChanged()"), self.unreadCountChanged)

    def setupUi(self):
        self.pixmapBtn = QtGui.QPushButton(self)
        self.pixmapBtn.setGeometry(QtCore.QRect(0, 0, 41, 41))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("play.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pixmapBtn.setIconSize(QtCore.QSize(32, 32))
        self.pixmapBtn.setIcon(icon)

        upBtn = QtGui.QPushButton("+", self)
        upBtn.setGeometry(QtCore.QRect(60, 0, 41, 41))
        self.connect(upBtn, QtCore.SIGNAL("clicked()"), self.onUpClicked)

        downBtn = QtGui.QPushButton("-", self)
        downBtn.setGeometry(QtCore.QRect(60, 50, 41, 41))
        self.connect(downBtn, QtCore.SIGNAL("clicked()"), self.onDownClicked)

        self.unreadLabel = QtGui.QLabel(self)
        self.unreadLabel.setText("Count: {}".format(self.unreadCount))
        self.unreadLabel.setGeometry(QtCore.QRect(5, 60, 51, 16))

        self.resize(200, 200)

    def onUpClicked(self):
        self.unreadCount += 1
        self.emit(QtCore.SIGNAL("unreadCountChanged()"))

    def onDownClicked(self):
        if self.unreadCount > 0:
            self.unreadCount -= 1
        self.emit(QtCore.SIGNAL("unreadCountChanged()"))

    def unreadCountChanged(self):
        self.unreadLabel.setText("Count: {}".format(self.unreadCount))

        pixmap = QtGui.QPixmap("play.png")
        if self.unreadCount > 0:
            painter = QtGui.QPainter()
            painter.begin(pixmap)
            painter.setBrush(QtCore.Qt.blue)  # Set the circle color
            center = QtCore.QPoint(90, 90)
            painter.drawEllipse(center, 40, 40)
            font = painter.font()
            font.setPointSize(30)
            pen = painter.pen()
            pen.setColor(QtCore.Qt.white)  # Set the text color
            painter.setPen(pen)
            painter.setFont(font)
            painter.drawText(80, 100, str(self.unreadCount))
            painter.end()

        icon = QtGui.QIcon()
        icon.addPixmap(pixmap, QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pixmapBtn.setIcon(icon)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = MyApplication()
    w.show()
    sys.exit(app.exec_())