2023-11-15 21:57:29 +08:00
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from PyQt5.Qt import *
|
|
|
|
|
from PyQt5.QtCore import *
|
|
|
|
|
from PyQt5.QtWidgets import *
|
|
|
|
|
|
|
|
|
|
from .CAvatar import CAvatar
|
|
|
|
|
|
2023-12-14 22:24:35 +08:00
|
|
|
|
Stylesheet = """
|
|
|
|
|
QWidget{
|
|
|
|
|
background: rgb(238,244,249);
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
Stylesheet_hover = """
|
|
|
|
|
QWidget,QLabel{
|
|
|
|
|
background: rgb(230, 235, 240);
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
Stylesheet_clicked = """
|
|
|
|
|
QWidget,QLabel{
|
|
|
|
|
background: rgb(230, 235, 240);
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QListWidgetItemWidget(QWidget):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.is_selected = False
|
|
|
|
|
|
|
|
|
|
def leaveEvent(self, e): # 鼠标离开label
|
|
|
|
|
if self.is_selected:
|
|
|
|
|
return
|
|
|
|
|
self.setStyleSheet(Stylesheet)
|
|
|
|
|
|
|
|
|
|
def enterEvent(self, e): # 鼠标移入label
|
|
|
|
|
self.setStyleSheet(Stylesheet_hover)
|
|
|
|
|
|
2023-11-15 21:57:29 +08:00
|
|
|
|
|
|
|
|
|
# 自定义的item 继承自QListWidgetItem
|
|
|
|
|
class ContactQListWidgetItem(QListWidgetItem):
|
2023-11-15 22:32:11 +08:00
|
|
|
|
def __init__(self, name, url, img_bytes=None):
|
2023-11-15 21:57:29 +08:00
|
|
|
|
super().__init__()
|
|
|
|
|
# 自定义item中的widget 用来显示自定义的内容
|
2023-12-14 22:24:35 +08:00
|
|
|
|
self.widget = QListWidgetItemWidget()
|
2023-11-15 21:57:29 +08:00
|
|
|
|
# 用来显示name
|
2023-12-14 22:24:35 +08:00
|
|
|
|
self.nameLabel = QLabel(self.widget)
|
2023-11-15 21:57:29 +08:00
|
|
|
|
self.nameLabel.setText(name)
|
|
|
|
|
# 用来显示avator(图像)
|
2023-12-14 22:24:35 +08:00
|
|
|
|
self.avatorLabel = CAvatar(parent=self.widget, shape=CAvatar.Rectangle, size=QSize(60, 60),
|
2023-11-15 22:32:11 +08:00
|
|
|
|
url=url, img_bytes=img_bytes)
|
2023-11-15 21:57:29 +08:00
|
|
|
|
# 设置布局用来对nameLabel和avatorLabel进行布局
|
2023-12-14 22:24:35 +08:00
|
|
|
|
hbox = QHBoxLayout()
|
|
|
|
|
hbox.addWidget(self.avatorLabel)
|
|
|
|
|
hbox.addWidget(self.nameLabel)
|
|
|
|
|
hbox.addStretch(1)
|
2023-11-15 21:57:29 +08:00
|
|
|
|
# 设置widget的布局
|
2023-12-14 22:24:35 +08:00
|
|
|
|
self.widget.setLayout(hbox)
|
|
|
|
|
self.widget.setStyleSheet(Stylesheet)
|
2023-11-15 21:57:29 +08:00
|
|
|
|
# 设置自定义的QListWidgetItem的sizeHint,不然无法显示
|
|
|
|
|
self.setSizeHint(self.widget.sizeHint())
|
|
|
|
|
|
2023-12-14 22:24:35 +08:00
|
|
|
|
def select(self):
|
|
|
|
|
"""
|
|
|
|
|
设置选择后的事件
|
|
|
|
|
@return:
|
|
|
|
|
"""
|
|
|
|
|
self.widget.is_selected = True
|
|
|
|
|
self.widget.setStyleSheet(Stylesheet_clicked)
|
|
|
|
|
|
|
|
|
|
def dis_select(self):
|
|
|
|
|
"""
|
|
|
|
|
设置取消选择的事件
|
|
|
|
|
@return:
|
|
|
|
|
"""
|
|
|
|
|
self.widget.is_selected = False
|
|
|
|
|
self.widget.setStyleSheet(Stylesheet)
|
|
|
|
|
|
2023-11-15 21:57:29 +08:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
|
|
|
|
|
# 主窗口
|
|
|
|
|
w = QWidget()
|
|
|
|
|
w.setWindowTitle("QListWindow")
|
|
|
|
|
# 新建QListWidget
|
|
|
|
|
listWidget = QListWidget(w)
|
|
|
|
|
listWidget.resize(300, 300)
|
|
|
|
|
|
|
|
|
|
# 新建两个自定义的QListWidgetItem(customQListWidgetItem)
|
|
|
|
|
item1 = ContactQListWidgetItem("鲤鱼王", "liyuwang.jpg")
|
|
|
|
|
item2 = ContactQListWidgetItem("可达鸭", "kedaya.jpg")
|
|
|
|
|
|
|
|
|
|
# 在listWidget中加入两个自定义的item
|
|
|
|
|
listWidget.addItem(item1)
|
|
|
|
|
listWidget.setItemWidget(item1, item1.widget)
|
|
|
|
|
listWidget.addItem(item2)
|
|
|
|
|
listWidget.setItemWidget(item2, item2.widget)
|
|
|
|
|
|
|
|
|
|
# 绑定点击槽函数 点击显示对应item中的name
|
|
|
|
|
listWidget.itemClicked.connect(lambda item: print(item.nameLabel.text()))
|
|
|
|
|
|
|
|
|
|
w.show()
|
|
|
|
|
sys.exit(app.exec_())
|