121 lines
3.7 KiB
Python
121 lines
3.7 KiB
Python
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QFrame, QLabel, QScrollArea
|
|
from PySide6.QtCore import Qt
|
|
import sys
|
|
|
|
class FriendList(QWidget):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
# 使用 QVBoxLayout 布局
|
|
self.layout = QVBoxLayout()
|
|
self.layout.setSpacing(0)
|
|
self.layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
# 添加好友分组
|
|
self.group1 = self.create_group("我的好友", ["小明", "小红", "小李"])
|
|
self.group2 = self.create_group("同学", ["张三", "李四", "王五"])
|
|
self.group3 = self.create_group("家人", ["爸爸", "妈妈", "姐姐"])
|
|
|
|
# 将每个分组添加到主布局
|
|
self.layout.addWidget(self.group1)
|
|
self.layout.addWidget(self.group2)
|
|
self.layout.addWidget(self.group3)
|
|
|
|
# 设置主布局
|
|
self.setLayout(self.layout)
|
|
|
|
def create_group(self, group_name, friends):
|
|
# 创建一个 QFrame 作为分组的容器
|
|
group_frame = QFrame()
|
|
group_layout = QVBoxLayout()
|
|
group_layout.setSpacing(0)
|
|
group_layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
# 创建分组按钮
|
|
group_button = QPushButton(group_name)
|
|
group_button.setObjectName("groupButton")
|
|
group_button.setCheckable(True)
|
|
group_button.setChecked(False) # 默认不展开
|
|
group_button.clicked.connect(lambda: self.toggle_group(group_frame)) # 切换菜单显示
|
|
|
|
# 创建好友列表
|
|
friend_frame = QFrame()
|
|
friend_layout = QVBoxLayout()
|
|
friend_layout.setSpacing(0)
|
|
friend_layout.setContentsMargins(20, 0, 0, 0) # 设置左边距,使其与分组按钮有明显缩进
|
|
|
|
for friend in friends:
|
|
friend_label = QLabel(friend)
|
|
friend_label.setObjectName("friendLabel")
|
|
friend_layout.addWidget(friend_label)
|
|
|
|
# 设置好友列表布局并隐藏
|
|
friend_frame.setLayout(friend_layout)
|
|
friend_frame.setVisible(False) # 默认隐藏
|
|
|
|
# 将按钮和好友列表添加到分组布局
|
|
group_layout.addWidget(group_button)
|
|
group_layout.addWidget(friend_frame)
|
|
group_frame.setLayout(group_layout)
|
|
|
|
# 绑定子菜单,用于后续切换显示
|
|
group_frame.menu = friend_frame
|
|
return group_frame
|
|
|
|
def toggle_group(self, group_frame):
|
|
# 切换好友列表的显示和隐藏
|
|
group_frame.menu.setVisible(not group_frame.menu.isVisible())
|
|
|
|
if __name__ == "__main__":
|
|
app = QApplication(sys.argv)
|
|
|
|
# 创建主窗口
|
|
window = QWidget()
|
|
layout = QVBoxLayout()
|
|
|
|
# 创建 FriendList 控件
|
|
friend_list = FriendList()
|
|
|
|
# 将 FriendList 放入滚动区域
|
|
scroll_area = QScrollArea()
|
|
scroll_area.setWidgetResizable(True)
|
|
scroll_area.setWidget(friend_list)
|
|
|
|
# 将滚动区域添加到主布局
|
|
layout.addWidget(scroll_area)
|
|
window.setLayout(layout)
|
|
|
|
# 设置窗口大小
|
|
window.resize(300, 500)
|
|
window.show()
|
|
|
|
# 使用 QSS 设置样式,模拟类似 QQ 好友列表的样式
|
|
app.setStyleSheet("""
|
|
QPushButton#groupButton {
|
|
background-color: #3A9;
|
|
border: none;
|
|
padding: 10px;
|
|
font-size: 16px;
|
|
color: white;
|
|
text-align: left;
|
|
}
|
|
QPushButton#groupButton:checked {
|
|
background-color: #5BB;
|
|
}
|
|
QLabel#friendLabel {
|
|
background-color: #EEE;
|
|
padding: 5px;
|
|
font-size: 14px;
|
|
color: black;
|
|
text-align: left;
|
|
}
|
|
QLabel#friendLabel:hover {
|
|
background-color: #CCC;
|
|
}
|
|
QFrame {
|
|
background-color: #FFF;
|
|
}
|
|
""")
|
|
|
|
sys.exit(app.exec())
|