回答編集履歴

2

d

2019/01/25 06:35

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -139,3 +139,127 @@
139
139
 
140
140
 
141
141
  ```
142
+
143
+
144
+
145
+ ## 追記
146
+
147
+
148
+
149
+ ```python
150
+
151
+ import sys
152
+
153
+ from PyQt5 import QtCore, QtGui, QtWidgets
154
+
155
+
156
+
157
+
158
+
159
+ class widgetA(QtWidgets.QWidget):
160
+
161
+
162
+
163
+ procStart = QtCore.pyqtSignal(str)
164
+
165
+
166
+
167
+ def __init__(self, modify, parent=None):
168
+
169
+ super(widgetA, self).__init__(parent)
170
+
171
+
172
+
173
+ self.modify = modify
174
+
175
+ self.tex = ""
176
+
177
+ self.lineEdit = QtWidgets.QLineEdit(self)
178
+
179
+ self.lineEdit.setText("Hello!")
180
+
181
+
182
+
183
+ self.button = QtWidgets.QPushButton("Send Message to B", self)
184
+
185
+ self.button.clicked.connect(self.on_button_clicked1)
186
+
187
+
188
+
189
+ self.layout = QtWidgets.QHBoxLayout(self)
190
+
191
+ self.layout.addWidget(self.lineEdit)
192
+
193
+ self.layout.addWidget(self.button)
194
+
195
+
196
+
197
+ def on_button_clicked1(self):
198
+
199
+ text = self.modify(self.lineEdit.text())
200
+
201
+ self.procStart.emit(text)
202
+
203
+
204
+
205
+
206
+
207
+ class widgetB(QtWidgets.QWidget):
208
+
209
+ def __init__(self, parent=None):
210
+
211
+ super(widgetB, self).__init__(parent)
212
+
213
+
214
+
215
+ self.lineEdit = QtWidgets.QLineEdit(self)
216
+
217
+ self.button = QtWidgets.QPushButton("Send Message to A", self)
218
+
219
+ self.layout = QtWidgets.QHBoxLayout(self)
220
+
221
+ self.layout.addWidget(self.lineEdit)
222
+
223
+ self.layout.addWidget(self.button)
224
+
225
+
226
+
227
+ def on_procStart(self, message):
228
+
229
+ self.lineEdit.setText("From A: " + message)
230
+
231
+ print(message)
232
+
233
+
234
+
235
+
236
+
237
+ if __name__ == "__main__":
238
+
239
+ import sys
240
+
241
+ app = QtWidgets.QApplication(sys.argv)
242
+
243
+
244
+
245
+ def modify(message):
246
+
247
+ return message + 'hogehoge'
248
+
249
+
250
+
251
+ widgetA = widgetA(modify)
252
+
253
+ widgetB = widgetB()
254
+
255
+ widgetA.show()
256
+
257
+ widgetB.show()
258
+
259
+ widgetA.procStart.connect(widgetB.on_procStart)
260
+
261
+
262
+
263
+ sys.exit(app.exec_())
264
+
265
+ ```

1

d

2019/01/25 06:35

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -12,9 +12,9 @@
12
12
 
13
13
  上記のような「あるオブジェクトのアクションが発生したら、別のオブジェクトでなにかアクションを起こす」というケースが GUI の開発ではよく出てきます。
14
14
 
15
- 例えば、matplotlib のように GUI 部分の実装 (バックエンドとして Qt も使われています) はライブラリ内に完全に隠蔽してライブラリ利用者側が直接触らないということであれば別ですが、
15
+ matplotlib のように GUI 部分の実装 (バックエンドとして Qt も使われています) はライブラリ内に完全に隠蔽してライブラリ利用者側が直接触らないということであれば別ですが、
16
16
 
17
- Qt を使用したライブラリを提供するのであれば、シグナルスロットという仕組みは隠蔽するのではなく、ライブラリ使用者側に理解して使ってもらうべきだと思います。
17
+ そうでなければ Qt を使用したライブラリを提供するのであれば、シグナルスロットという仕組みは隠蔽するのではなく、ライブラリ使用者側に理解して使ってもらうべきだと思います。
18
18
 
19
19
 
20
20