/
withpubsub.py
109 lines (77 loc) · 2.66 KB
/
withpubsub.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
"""
Three layers of widgets, inner widget communicating with
outer widget.
_______________________
| Top |
| _________________ |
| | List | |
| | ___________ | |
| | | Item | | |
| | | | | |
| | |___________| | |
| |_________________| |
|_______________________|
qtpath://Top.QWidget/List1.QWidget/Item1.QWidget
Description:
In this example, `pubsub` acts as a broker; forwarding events
based on their target address/topic/frequency.
Note that List is de-coupled from any communication
between `Item` and `Top`.
"""
from __future__ import absolute_import
from PyQt5 import QtWidgets
import qtpath
import pubsub.pub
class Item(QtWidgets.QPushButton):
def __init__(self, *args, **kwargs):
super(Item, self).__init__(*args, **kwargs)
self.pressed.connect(self.locations_event)
def locations_event(self):
root = self.window()
path = qtpath.abspath(root, self)
pubsub.pub.sendMessage('locations', path=path)
def mouseDoubleClickEvent(self, event):
message = "Hello from %s" % self.objectName()
pubsub.pub.sendMessage('messages', message=message)
class List(QtWidgets.QWidget):
def __init__(self, name, parent=None):
super(List, self).__init__(parent)
header = QtWidgets.QWidget()
label = QtWidgets.QLabel(name)
layout = QtWidgets.QHBoxLayout(header)
layout.addWidget(label)
body = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(body)
for x in range(10):
name = 'Item%i' % x
item = Item(name)
item.setObjectName(name)
layout.addWidget(item)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(header)
layout.addWidget(body)
class Top(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(Top, self).__init__(*args, **kwargs)
self.setObjectName('Main')
body = QtWidgets.QWidget()
layout = QtWidgets.QHBoxLayout(body)
for x in range(3):
name = 'List%i' % x
lis = List(name)
lis.setObjectName(name)
layout.addWidget(lis)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(body)
pubsub.pub.subscribe(self.locations_event, 'locations')
pubsub.pub.subscribe(self.messages_event, 'messages')
def locations_event(self, path):
print "%s was received from Top" % path
def messages_event(self, message):
print message
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
win = Top()
win.show()
app.exec_()