簡體   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