QListView

This commit is contained in:
Irony 2018-08-04 01:13:39 +08:00
parent 5b741d0f20
commit 21a9f35fc5
7 changed files with 195 additions and 0 deletions

View file

@ -19,10 +19,13 @@ encoding//QGraphicsView\u7EC3\u4E60/\u6DFB\u52A0QWidget.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u7B80\u5355\u56FE\u50CF\u5904\u7406/SimpleImagePs.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u7B80\u5355\u56FE\u50CF\u5904\u7406/SimpleImageThread.py=utf-8
encoding//QGraphicsView\u7EC3\u4E60/\u7B80\u5355\u56FE\u50CF\u5904\u7406/SimpleImageView.py=utf-8
encoding//QListView/\u663E\u793A\u81EA\u5B9A\u4E49Widget.py=utf-8
encoding//QListView/\u663E\u793A\u81EA\u5B9A\u4E49Widget\u5E76\u6392\u5E8F.py=utf-8
encoding//QRC\u8D44\u6E90\u6587\u4EF6\u4F7F\u7528/qrctest1.py=utf-8
encoding//QRC\u8D44\u6E90\u6587\u4EF6\u4F7F\u7528/qrctest2.py=utf-8
encoding//QRC\u8D44\u6E90\u6587\u4EF6\u4F7F\u7528/res_rc.py=utf-8
encoding//QRC\u8D44\u6E90\u6587\u4EF6\u4F7F\u7528/textread.py=utf-8
encoding//tmp/ListView.py=utf-8
encoding//tmp/LocalClient.py=utf-8
encoding//tmp/Material/Effect/LineEffect.py=utf-8
encoding//tmp/Material/Utils/Colors.py=utf-8
@ -32,6 +35,7 @@ encoding//tmp/Material/test/TestLineEdit.py=utf-8
encoding//tmp/QtServer.py=utf-8
encoding//tmp/error.py=utf-8
encoding//tmp/json\u751F\u6210\u6811\u5F62\u7ED3\u6784.py=utf-8
encoding//tmp/\u52A0\u8F7Dui\u63CF\u7ED8\u8FB9\u754C.py=utf-8
encoding//tmp/\u622A\u56FE\u753B\u77E9\u5F62/DrawRectangle.py=utf-8
encoding//tmp/\u7B80\u5355\u63D2\u4EF6/Widget.py=utf-8
encoding//tmp/\u8BFB\u53D6\u526A\u5207\u677F\u56FE\u7247.py=utf-8

11
QListView/README.md Normal file
View file

@ -0,0 +1,11 @@
# QListView练习
显示自定义widget使用setIndexWidget
排序使用QSortFilterProxyModel
## 截图
![截图](ScreenShot/1.png)
![截图](ScreenShot/2.gif)

BIN
QListView/ScreenShot/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
QListView/ScreenShot/2.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 KiB

View file

@ -0,0 +1,63 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QListView, QWidget, QHBoxLayout, QLineEdit,\
QPushButton
# Created on 2018年8月4日
# author: Irony
# site: https://github.com/892768447
# email: 892768447@qq.com
# file: QListView.显示自定义Widget
# description:
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = 'Copyright (c) 2018 Irony'
__Version__ = 1.0
class CustomWidget(QWidget):
def __init__(self, text, *args, **kwargs):
super(CustomWidget, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(QLineEdit(text, self))
layout.addWidget(QPushButton('x', self))
def sizeHint(self):
# 决定item的高度
return QSize(200, 40)
class ListView(QListView):
def __init__(self, *args, **kwargs):
super(ListView, self).__init__(*args, **kwargs)
# 模型
self._model = QStandardItemModel(self)
self.setModel(self._model)
# 循环生成10个自定义控件
for i in range(10):
item = QStandardItem()
self._model.appendRow(item) # 添加item
# 得到索引
index = self._model.indexFromItem(item)
widget = CustomWidget(str(i))
item.setSizeHint(widget.sizeHint()) # 主要是调整item的高度
# 设置自定义的widget
self.setIndexWidget(index, widget)
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = ListView()
w.show()
sys.exit(app.exec_())

View file

@ -0,0 +1,116 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from random import choice, randint
import string
from time import time
from PyQt5.QtCore import QSortFilterProxyModel, Qt, QSize
from PyQt5.QtGui import QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QListView,\
QHBoxLayout, QLineEdit
# Created on 2018年8月4日
# author: Irony
# site: https://github.com/892768447
# email: 892768447@qq.com
# file: QListView.显示自定义Widget并排序
# description:
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = 'Copyright (c) 2018 Irony'
__Version__ = 1.0
def randomChar(y):
# 返回随机字符串
return ''.join(choice(string.ascii_letters) for _ in range(y))
class CustomWidget(QWidget):
def __init__(self, text, *args, **kwargs):
super(CustomWidget, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(QLineEdit(text, self))
layout.addWidget(QPushButton('x', self))
def sizeHint(self):
# 决定item的高度
return QSize(200, 40)
class SortFilterProxyModel(QSortFilterProxyModel):
def lessThan(self, source_left, source_right):
if not source_left.isValid() or not source_right.isValid():
return False
# 获取数据
leftData = self.sourceModel().data(source_left)
rightData = self.sourceModel().data(source_right)
if self.sortOrder() == Qt.DescendingOrder:
# 按照时间倒序排序
leftData = leftData.split('-')[-1]
rightData = rightData.split('-')[-1]
return leftData < rightData
# elif self.sortOrder() == Qt.AscendingOrder:
# #按照名字升序排序
# leftData = leftData.split('-')[0]
# rightData = rightData.split('-')[0]
# return leftData < rightData
return super(SortFilterProxyModel, self).lessThan(source_left, source_right)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.resize(800, 600)
layout = QVBoxLayout(self)
# 名字排序
layout.addWidget(QPushButton('以名字升序', self, clicked=self.sortByName))
# 时间倒序
layout.addWidget(QPushButton('以时间倒序', self, clicked=self.sortByTime))
# listview
self.listView = QListView(self)
layout.addWidget(self.listView)
# 数据模型
self.dmodel = QStandardItemModel(self.listView)
# 排序代理模型
self.fmodel = SortFilterProxyModel(self.listView)
self.fmodel.setSourceModel(self.dmodel)
self.listView.setModel(self.fmodel)
# 模拟生成50条数据
for _ in range(50):
name = randomChar(5)
times = time() + randint(0, 30) # 当前时间随机+
value = '{}-{}'.format(name, times) # 内容用-分开
item = QStandardItem(value)
# item.setData(value, Qt.UserRole + 2)
self.dmodel.appendRow(item)
# 索引
index = self.fmodel.mapFromSource(item.index())
# 自定义的widget
widget = CustomWidget(value, self)
item.setSizeHint(widget.sizeHint())
self.listView.setIndexWidget(index, widget)
def sortByTime(self):
# 按照时间倒序排序
self.fmodel.sort(0, Qt.DescendingOrder)
def sortByName(self):
# 按照名字升序排序
self.fmodel.sort(0, Qt.AscendingOrder)
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

View file

@ -45,6 +45,7 @@
- [1.41 消息对话框倒计时关闭](消息对话框倒计时关闭/)
- 1.42 悬浮下拉菜单
- [1. tableWidget形式](partner_625781186/5.hoverMenu)
- [1.43 QListView](QListView/)
### [2.QGraphicsView练习](QGraphicsView练习/)
- [2.1 世界地图](QGraphicsView练习/世界地图)