繁体   English   中英

PySide2-如何在标签上显示“未读数量”?

[英]PySide2 - How can I show the “count of unread” on label?

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

还是这实际上是在同一块中显示两个标签?

在此处输入图片说明

感谢@LoïcG.,根据您提供的关键字,然后在下面编写一些代码,

可能效果很好。 但是在重画之前如何擦除?

在此处输入图片说明

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个按钮的工作示例,以增加/减少“未读”值。

每次更改此值时, unreadCountChanged()发出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_())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM