列表常用信号

This commit is contained in:
Irony 2019-07-03 15:00:16 +08:00
parent c7a97a3abb
commit ea89fff811
5 changed files with 136 additions and 1 deletions

View file

@ -31,6 +31,7 @@ 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//QListWidget/SignalsExample.py=utf-8
encoding//QMessageBox/CustomColorIcon.py=utf-8
encoding//QProgressBar/Lib/WaterRippleProgressBar.py=utf-8
encoding//QProgressBar/MetroCircleProgress.py=utf-8

View file

@ -5,6 +5,7 @@
- [自定义可拖拽Item](#2自定义可拖拽Item)
- [腾讯视频热播列表](#3腾讯视频热播列表)
- [仿折叠控件效果](#4仿折叠控件效果)
- [列表常用信号](#5列表常用信号)
## 1、删除自定义Item
[运行 DeleteCustomItem.py](DeleteCustomItem.py)
@ -53,3 +54,10 @@
4. 自定义控件中尺寸发生变化后需要调用`adjustSize()`来同步
![FoldWidget](ScreenShot/FoldWidget.gif)
## 5、列表常用信号
[运行 SignalsExample.py](SignalsExample.py)
根据官网文档 https://doc.qt.io/qt-5/qlistwidget.html#signals 中的信号介绍编写
![SignalsExample](ScreenShot/SignalsExample.gif)

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

View file

@ -0,0 +1,125 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年7月3日
@author: Irony
@site: https://pyqt5.com https://github.com/PyQt5
@email: 892768447@qq.com
@file: QListWidget.SignalsExample
@description:
"""
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QListWidget, QPlainTextEdit,\
QListWidgetItem, QAbstractItemView, QListView
__Author__ = "Irony"
__Copyright__ = "Copyright (c) 2019"
__Version__ = "Version 1.0"
def formatColor(text, color):
return '<font color={0}>{1}</font>'.format(color.name(), text)
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
self.listWidget = QListWidget(self)
self.listWidget.setAlternatingRowColors(True)
self.listWidget.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.listWidget.setMovement(QListView.Free)
self.listWidget.setMouseTracking(True) # 用于itemEntered信号
self.resultView = QPlainTextEdit(self)
self.resultView.setReadOnly(True)
layout.addWidget(self.listWidget)
layout.addWidget(self.resultView)
self.initData()
self.initSignals()
def initData(self):
# 初始化模拟数据
for i in range(100):
item = QListWidgetItem('Item {0}'.format(i), self.listWidget)
if i % 3 == 0:
item.setFlags(item.flags() | Qt.ItemIsEditable)
def initSignals(self):
# 初始化信号
self.listWidget.currentItemChanged.connect(self.onCurrentItemChanged)
self.listWidget.currentRowChanged.connect(self.onCurrentRowChanged)
self.listWidget.currentTextChanged.connect(self.onCurrentTextChanged)
self.listWidget.itemActivated.connect(self.onItemActivated)
self.listWidget.itemChanged.connect(self.onItemChanged)
self.listWidget.itemClicked.connect(self.onItemClicked)
self.listWidget.itemDoubleClicked.connect(self.onItemDoubleClicked)
self.listWidget.itemEntered.connect(self.onItemEntered)
self.listWidget.itemPressed.connect(self.onItemPressed)
self.listWidget.itemSelectionChanged.connect(
self.onItemSelectionChanged)
def onCurrentItemChanged(self, current, previous):
current = current.text() if current else ''
previous = previous.text() if previous else ''
self.resultView.appendHtml(
'{0}: [{1}] -> [{2}]'.format(
formatColor('currentItemChanged', QColor(Qt.red)),
current, previous))
def onCurrentRowChanged(self, currentRow):
self.resultView.appendHtml(
'{0}: {1}'.format(
formatColor('currentRowChanged', QColor(Qt.green)),
currentRow))
def onCurrentTextChanged(self, currentText):
self.resultView.appendHtml(
'{0}: {1}'.format(
formatColor('currentTextChanged', QColor(Qt.yellow)), currentText))
def onItemActivated(self, item):
self.resultView.appendHtml(
'{0}: {1}'.format(
formatColor('itemActivated', QColor(Qt.blue)), item.text()))
def onItemChanged(self, item):
self.resultView.appendHtml(
'{0}: {1}'.format(
formatColor('itemChanged', QColor(Qt.cyan)), item.text()))
def onItemClicked(self, item):
self.resultView.appendHtml(
'{0}: {1}'.format(formatColor('itemClicked', QColor(Qt.magenta)), item.text()))
def onItemDoubleClicked(self, item):
self.resultView.appendHtml(
'{0}: {1}'.format(formatColor('itemDoubleClicked', QColor(Qt.darkGreen)), item.text()))
def onItemEntered(self, item):
self.resultView.appendHtml(
'{0}: {1}'.format(formatColor('itemEntered', QColor(Qt.darkCyan)), item.text()))
def onItemPressed(self, item):
print(item)
self.resultView.appendHtml(
'{0}: {1}'.format(formatColor('itemPressed', QColor(Qt.darkYellow)), item.text()))
def onItemSelectionChanged(self):
self.resultView.appendPlainText('itemSelectionChanged')
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

View file

@ -60,6 +60,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
- [自定义可拖拽Item](QListWidget/DragDrop.py)
- [腾讯视频热播列表](QListWidget/HotPlaylist.py)
- [仿折叠控件效果](QListWidget/FoldWidget.py)
- [列表常用信号](QListWidget/SignalsExample.py)
- [在item中添加图标](Test/partner_625781186/13.combo_listwidget)
- [QTreeWidget](QTreeWidget)
- [通过json数据生成树形结构](QTreeWidget/ParsingJson.py)