gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:25:22 27 4
gpt4 key购买 nike

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

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

enter image description here

最佳答案

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

下面,您将找到一个工作示例,其中有 2 个按钮用于增加/减少“未读”值。

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

关于python - PySide2 - 如何在标签上显示 "count of unread"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47475627/

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