仿折叠控件效果
This commit is contained in:
parent
676b0a4139
commit
3f2dcd7ad1
5 changed files with 103 additions and 1 deletions
|
@ -30,6 +30,7 @@ encoding//QGraphicsDropShadowEffect/ShadowEffect.py=utf-8
|
||||||
encoding//QGraphicsView/WorldMap.py=utf-8
|
encoding//QGraphicsView/WorldMap.py=utf-8
|
||||||
encoding//QListView/CustomWidgetSortItem.py=utf-8
|
encoding//QListView/CustomWidgetSortItem.py=utf-8
|
||||||
encoding//QListView/SortItemByRole.py=utf-8
|
encoding//QListView/SortItemByRole.py=utf-8
|
||||||
|
encoding//QListWidget/FoldWidget.py=utf-8
|
||||||
encoding//QMessageBox/CustomColorIcon.py=utf-8
|
encoding//QMessageBox/CustomColorIcon.py=utf-8
|
||||||
encoding//QProgressBar/Lib/WaterRippleProgressBar.py=utf-8
|
encoding//QProgressBar/Lib/WaterRippleProgressBar.py=utf-8
|
||||||
encoding//QProgressBar/MetroCircleProgress.py=utf-8
|
encoding//QProgressBar/MetroCircleProgress.py=utf-8
|
||||||
|
|
89
QListWidget/FoldWidget.py
Normal file
89
QListWidget/FoldWidget.py
Normal file
|
@ -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_())
|
|
@ -4,6 +4,7 @@
|
||||||
- [删除自定义Item](#1删除自定义Item)
|
- [删除自定义Item](#1删除自定义Item)
|
||||||
- [自定义可拖拽Item](#2自定义可拖拽Item)
|
- [自定义可拖拽Item](#2自定义可拖拽Item)
|
||||||
- [腾讯视频热播列表](#3腾讯视频热播列表)
|
- [腾讯视频热播列表](#3腾讯视频热播列表)
|
||||||
|
- [仿折叠控件效果](#4仿折叠控件效果)
|
||||||
|
|
||||||
## 1、删除自定义Item
|
## 1、删除自定义Item
|
||||||
[运行 DeleteCustomItem.py](DeleteCustomItem.py)
|
[运行 DeleteCustomItem.py](DeleteCustomItem.py)
|
||||||
|
@ -42,3 +43,13 @@
|
||||||
3. `setResizeMode(QListWidget.Adjust)`
|
3. `setResizeMode(QListWidget.Adjust)`
|
||||||
|
|
||||||
![HotPlaylist](ScreenShot/HotPlaylist.gif)
|
![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)
|
BIN
QListWidget/ScreenShot/FoldWidget.gif
Normal file
BIN
QListWidget/ScreenShot/FoldWidget.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 171 KiB |
|
@ -54,6 +54,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
|
||||||
- [删除自定义Item](QListWidget/DeleteCustomItem.py)
|
- [删除自定义Item](QListWidget/DeleteCustomItem.py)
|
||||||
- [自定义可拖拽Item](QListWidget/DragDrop.py)
|
- [自定义可拖拽Item](QListWidget/DragDrop.py)
|
||||||
- [腾讯视频热播列表](QListWidget/HotPlaylist.py)
|
- [腾讯视频热播列表](QListWidget/HotPlaylist.py)
|
||||||
|
- [仿折叠控件效果](QListWidget/FoldWidget.py)
|
||||||
- [在item中添加图标](Test/partner_625781186/13.combo_listwidget)
|
- [在item中添加图标](Test/partner_625781186/13.combo_listwidget)
|
||||||
- [QTreeWidget](QTreeWidget)
|
- [QTreeWidget](QTreeWidget)
|
||||||
- [通过json数据生成树形结构](QTreeWidget/ParsingJson.py)
|
- [通过json数据生成树形结构](QTreeWidget/ParsingJson.py)
|
||||||
|
|
Loading…
Reference in a new issue