diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 30f0a4d..22bd885 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -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 diff --git a/QListWidget/README.md b/QListWidget/README.md index 62f3a80..a129209 100644 --- a/QListWidget/README.md +++ b/QListWidget/README.md @@ -5,6 +5,7 @@ - [自定义可拖拽Item](#2自定义可拖拽Item) - [腾讯视频热播列表](#3腾讯视频热播列表) - [仿折叠控件效果](#4仿折叠控件效果) + - [列表常用信号](#5列表常用信号) ## 1、删除自定义Item [运行 DeleteCustomItem.py](DeleteCustomItem.py) @@ -52,4 +53,11 @@ 3. 绑定按钮的选中状态通过`setHidden`设置Item的隐藏和显示 4. 自定义控件中尺寸发生变化后需要调用`adjustSize()`来同步 -![FoldWidget](ScreenShot/FoldWidget.gif) \ No newline at end of file +![FoldWidget](ScreenShot/FoldWidget.gif) + +## 5、列表常用信号 +[运行 SignalsExample.py](SignalsExample.py) + +根据官网文档 https://doc.qt.io/qt-5/qlistwidget.html#signals 中的信号介绍编写 + +![SignalsExample](ScreenShot/SignalsExample.gif) \ No newline at end of file diff --git a/QListWidget/ScreenShot/SignalsExample.gif b/QListWidget/ScreenShot/SignalsExample.gif new file mode 100644 index 0000000..37357e6 Binary files /dev/null and b/QListWidget/ScreenShot/SignalsExample.gif differ diff --git a/QListWidget/SignalsExample.py b/QListWidget/SignalsExample.py new file mode 100644 index 0000000..bb17e42 --- /dev/null +++ b/QListWidget/SignalsExample.py @@ -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 '{1}'.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_()) diff --git a/README.md b/README.md index 8da5ed1..c249347 100644 --- a/README.md +++ b/README.md @@ -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)