pyqt-todolist/view/SelfListWidgetItem.py

55 lines
1.9 KiB
Python
Raw Normal View History

2022-04-01 22:25:19 +08:00
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import sys
import os
class SelfListWidgetItem(QListWidgetItem):
"""
:param item_name: 列表名称
:param todo_count: 设置剩余代办数量默认为零
:param show_icon: 设置显示的图标路径默认为空
"""
def __init__(self, item_name, todo_count=0, show_icon=None):
super(SelfListWidgetItem, self).__init__()
layout = QHBoxLayout()
# print(show_icon)
self.item_name = item_name
self.todo_count = todo_count
self.show_icon = show_icon
# 布局
self.widget = QWidget()
self.widget.setLayout(layout)
# self.list_item = QLabel('')
# 添加左边的图标和右边的气泡
if self.show_icon:
self.icon_label = QLabel('')
self.icon_label.setPixmap(
QPixmap(self.show_icon).scaled(30, 30, Qt.IgnoreAspectRatio, Qt.SmoothTransformation))
layout.addWidget(self.icon_label, 1)
# 添加
self.item_name_label = QLabel(self.item_name)
# print(self.item_name)
self.item_name_label.setObjectName('item_name_label')
2022-04-07 19:34:28 +08:00
# todo 需要设置qss使字体靠中间一点,如果有气泡使气泡靠右 done
2022-04-01 22:25:19 +08:00
layout.addWidget(self.item_name_label, 3)
if self.todo_count:
# 打印出代办数目
print('todo_count:', self.todo_count)
2022-04-01 22:25:19 +08:00
self.todo_count_label = QLabel(str(self.todo_count))
self.todo_count_label.setFixedSize(16, 16)
2022-04-01 22:25:19 +08:00
self.todo_count_label.setScaledContents(True)
self.todo_count_label.setObjectName('todo_count_label')
self.todo_count_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.todo_count_label, 1)
# 设置自定义的QListWidgetItem的sizeHint不然无法显示
self.setSizeHint(self.widget.sizeHint())