forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SignalsExample.py
125 lines (99 loc) · 4.3 KB
/
SignalsExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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_())