diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 9b40e4d..75d5031 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -30,6 +30,7 @@ encoding//QGraphicsDropShadowEffect/ShadowEffect.py=utf-8 encoding//QGraphicsView/WorldMap.py=utf-8 encoding//QListView/CustomWidgetSortItem.py=utf-8 encoding//QListView/SortItemByRole.py=utf-8 +encoding//QListWidget/FoldWidget.py=utf-8 encoding//QMessageBox/CustomColorIcon.py=utf-8 encoding//QProgressBar/Lib/WaterRippleProgressBar.py=utf-8 encoding//QProgressBar/MetroCircleProgress.py=utf-8 diff --git a/QListWidget/FoldWidget.py b/QListWidget/FoldWidget.py new file mode 100644 index 0000000..d522c95 --- /dev/null +++ b/QListWidget/FoldWidget.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2019年5月27日 +@author: Irony +@site: https://pyqt5.com https://github.com/PyQt5 +@email: 892768447@qq.com +@file: FoldWidget +@description: 自定义item折叠控件仿QTreeWidget +""" + +from PyQt5.QtCore import QSize +from PyQt5.QtWidgets import QWidget, QPushButton, QFormLayout,\ + QLineEdit, QListWidget, QListWidgetItem, QCheckBox + + +__Author__ = "Irony" +__Copyright__ = "Copyright (c) 2019" +__Version__ = "Version 1.0" + + +class CustomWidget(QWidget): + + def __init__(self, item, *args, **kwargs): + super(CustomWidget, self).__init__(*args, **kwargs) + self.oldSize = None + self.item = item + layout = QFormLayout(self) + layout.addRow('我是label', QLineEdit(self)) + layout.addRow('点击', QCheckBox( + '隐藏下面的按钮', self, toggled=self.hideChild)) + self.button = QPushButton('我是被隐藏的', self) + layout.addRow(self.button) + + def hideChild(self, v): + self.button.setVisible(not v) + # 这里很重要 当隐藏内部子控件时 需要重新计算高度 + self.adjustSize() + + def resizeEvent(self, event): + # 解决item的高度问题 + super(CustomWidget, self).resizeEvent(event) + self.item.setSizeHint(QSize(self.minimumWidth(), self.height())) + + +class CustomButton(QPushButton): + # 按钮作为开关 + + def __init__(self, item, *args, **kwargs): + super(CustomButton, self).__init__(*args, **kwargs) + self.item = item + self.setCheckable(True) # 设置可选中 + + def resizeEvent(self, event): + # 解决item的高度问题 + super(CustomButton, self).resizeEvent(event) + self.item.setSizeHint(QSize(self.minimumWidth(), self.height())) + + +class Window(QListWidget): + + def __init__(self, *args, **kwargs): + super(Window, self).__init__(*args, **kwargs) + + for _ in range(3): + # 开关 + item = QListWidgetItem(self) + btn = CustomButton(item, '折叠', self, objectName='testBtn') + self.setItemWidget(item, btn) + + # 被折叠控件 + item = QListWidgetItem(self) + # 通过按钮的选中来隐藏下面的item + btn.toggled.connect(item.setHidden) + self.setItemWidget(item, CustomWidget(item, self)) + + +if __name__ == '__main__': + import sys + import cgitb + sys.excepthook = cgitb.enable(1, None, 5, '') + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + # 通过qss改变按钮的高度 + app.setStyleSheet('#testBtn{min-height:40px;}') + w = Window() + w.show() + sys.exit(app.exec_()) diff --git a/QListWidget/README.md b/QListWidget/README.md index c17b810..62f3a80 100644 --- a/QListWidget/README.md +++ b/QListWidget/README.md @@ -4,6 +4,7 @@ - [删除自定义Item](#1删除自定义Item) - [自定义可拖拽Item](#2自定义可拖拽Item) - [腾讯视频热播列表](#3腾讯视频热播列表) + - [仿折叠控件效果](#4仿折叠控件效果) ## 1、删除自定义Item [运行 DeleteCustomItem.py](DeleteCustomItem.py) @@ -41,4 +42,14 @@ 2. `setWrapping(True)` 3. `setResizeMode(QListWidget.Adjust)` -![HotPlaylist](ScreenShot/HotPlaylist.gif) \ No newline at end of file +![HotPlaylist](ScreenShot/HotPlaylist.gif) + +## 4、仿折叠控件效果 +[运行 FoldWidget.py](FoldWidget.py) + +1. 利用`QListWidget`设置Item的自定义控件 +2. `QListWidget`通过间隔设置`QPushButton`和`CustomWidget`来添加 +3. 绑定按钮的选中状态通过`setHidden`设置Item的隐藏和显示 +4. 自定义控件中尺寸发生变化后需要调用`adjustSize()`来同步 + +![FoldWidget](ScreenShot/FoldWidget.gif) \ No newline at end of file diff --git a/QListWidget/ScreenShot/FoldWidget.gif b/QListWidget/ScreenShot/FoldWidget.gif new file mode 100644 index 0000000..bf9a2ab Binary files /dev/null and b/QListWidget/ScreenShot/FoldWidget.gif differ diff --git a/README.md b/README.md index 728aeb4..a2e57b7 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站 - [删除自定义Item](QListWidget/DeleteCustomItem.py) - [自定义可拖拽Item](QListWidget/DragDrop.py) - [腾讯视频热播列表](QListWidget/HotPlaylist.py) + - [仿折叠控件效果](QListWidget/FoldWidget.py) - [在item中添加图标](Test/partner_625781186/13.combo_listwidget) - [QTreeWidget](QTreeWidget) - [通过json数据生成树形结构](QTreeWidget/ParsingJson.py)