回答編集履歴

3

2018/10/09 05:07

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -331,3 +331,15 @@
331
331
 
332
332
 
333
333
  [Qiita](https://qiita.com/search?q=qt)
334
+
335
+
336
+
337
+ * 公式の Example
338
+
339
+
340
+
341
+ Qt Creator をインストールするとかなりの数の Example がついてきます。
342
+
343
+
344
+
345
+ ![イメージ説明](756da2a47fd691c11fbad5a4c0bf87fd.png)

2

2018/10/09 05:07

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -281,3 +281,53 @@
281
281
  main()
282
282
 
283
283
  ```
284
+
285
+
286
+
287
+ ## 追記 Qt の情報源
288
+
289
+
290
+
291
+ C++ も PyQt もライブラリの使い方は同じで情報量は C++ のほうが多いので、検索するときは PyQt ではなく Qt で調べたほうがいいです。(C++ の文法を知らなくても大丈夫です。)
292
+
293
+
294
+
295
+ 検索は日本語で出てこなければ、英語のキーワードで調べたほうがいいです。Stack Overflow でだいたい既出です。
296
+
297
+
298
+
299
+ * [Qt のリファレンス](http://doc.qt.io/qt-5/classes.html)
300
+
301
+
302
+
303
+ 使用するクラスに関してだけ確認する。
304
+
305
+
306
+
307
+ * Stack Overflow / Qt Centre などの質問サイト
308
+
309
+
310
+
311
+ 例えば、「QGraphicsView の表示スケールを QGraphicsScene がすべて収まるように変更したい」と思った場合は「qgraphicsview auto scale」と検索してみると、
312
+
313
+
314
+
315
+ 1個目、2個目に答えが出てきます。
316
+
317
+
318
+
319
+ [Auto scale a QGraphicsView](https://www.qtcentre.org/threads/42917-Auto-scale-a-QGraphicsView)
320
+
321
+ [how to Fit QGraphicsScene in a QGraphicsView](https://stackoverflow.com/questions/10891962/how-to-fit-qgraphicsscene-in-a-qgraphicsview)
322
+
323
+
324
+
325
+ * Qiita などの日本語のサイト
326
+
327
+
328
+
329
+ 日本語の情報源もそれなりにあります。
330
+
331
+
332
+
333
+ [Qiita](https://qiita.com/search?q=qt)

1

a

2018/10/09 05:01

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -63,3 +63,221 @@
63
63
 
64
64
 
65
65
  すべての円が表示されるように Example クラスの `def boundingRect(self)` が返す Rect を動的に変更する必要があります。
66
+
67
+
68
+
69
+ ## 全部表示されるように変更したコード
70
+
71
+
72
+
73
+ * 原寸大で表示したい場合は fitInView() の2箇所をコメントアウトしてください。
74
+
75
+ * GGraphicsItem の大きさ等変更する場合 (scale の変更など) は変更前に prepareGeometryChange() をよんでください。変更が完了したら、update() で再描画すればよいです。
76
+
77
+
78
+
79
+ ```python
80
+
81
+ import sys
82
+
83
+ from PyQt5.QtWidgets import *
84
+
85
+ from PyQt5.QtGui import *
86
+
87
+ from PyQt5.QtCore import *
88
+
89
+
90
+
91
+
92
+
93
+ class Example(QGraphicsItem):
94
+
95
+ def __init__(self, width=400, height=400):
96
+
97
+ super(Example, self).__init__()
98
+
99
+
100
+
101
+ self.width = width
102
+
103
+ self.height = height
104
+
105
+ self.x_origin = width / 2
106
+
107
+ self.y_origin = height / 2
108
+
109
+ self.scale = 20
110
+
111
+
112
+
113
+ def paint(self, painter, option, widget):
114
+
115
+ painter.setPen(Qt.black)
116
+
117
+ painter.drawLine(0, self.y_origin, self.width, self.y_origin)
118
+
119
+ painter.drawLine(self.x_origin, 0, self.x_origin, self.height)
120
+
121
+ print("paint")
122
+
123
+
124
+
125
+ for i in range(100):
126
+
127
+ x = self.x_origin + i * self.scale
128
+
129
+ y = self.y_origin + i * self.scale
130
+
131
+ painter.setPen(Qt.black)
132
+
133
+ painter.setBrush(QBrush(Qt.red, Qt.SolidPattern))
134
+
135
+ painter.drawEllipse(QPointF(x, y), 9, 9)
136
+
137
+
138
+
139
+ def boundingRect(self):
140
+
141
+ width = self.y_origin + 100 * self.scale + 30
142
+
143
+ height = self.y_origin + 100 * self.scale + 30
144
+
145
+ return QRectF(0, 0, width, height)
146
+
147
+
148
+
149
+ def setScale(self, x):
150
+
151
+ self.prepareGeometryChange()
152
+
153
+ self.scale = x
154
+
155
+ self.update()
156
+
157
+
158
+
159
+ def getScale(self):
160
+
161
+ return self.scale
162
+
163
+
164
+
165
+
166
+
167
+ class MyWindow(QMainWindow):
168
+
169
+ def __init__(self):
170
+
171
+ super().__init__()
172
+
173
+ self.initUI()
174
+
175
+
176
+
177
+ def initUI(self):
178
+
179
+ self.setGeometry(50, 50, 600, 500)
180
+
181
+ self.setWindowTitle("Ex")
182
+
183
+ self.setWindow()
184
+
185
+ self.show()
186
+
187
+
188
+
189
+ def setWindow(self):
190
+
191
+ self.w = QWidget()
192
+
193
+ self.view = QGraphicsView()
194
+
195
+ self.scene = QGraphicsScene(self.view)
196
+
197
+ self.srect = self.view.rect()
198
+
199
+ width = self.srect.width()
200
+
201
+ height = self.srect.height()
202
+
203
+ # self.scene.setSceneRect(QRectF(self.srect))
204
+
205
+ self.graph = Example(width, height)
206
+
207
+ self.scene.addItem(self.graph)
208
+
209
+ self.view.setScene(self.scene)
210
+
211
+ self.view.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
212
+
213
+
214
+
215
+ label = QLabel(" X ")
216
+
217
+ self.box = QLineEdit()
218
+
219
+ x = self.getScale()
220
+
221
+ self.box.setText("{}".format(x))
222
+
223
+ update = QPushButton('Update')
224
+
225
+ update.clicked.connect(self.setScale)
226
+
227
+
228
+
229
+ main = QGridLayout()
230
+
231
+ main.addWidget(self.view, 0, 0, 30, 5)
232
+
233
+ main.addWidget(label, 0, 10, 1, 3)
234
+
235
+ main.addWidget(self.box, 1, 10, 1, 3)
236
+
237
+ main.addWidget(update, 2, 10, 1, 3)
238
+
239
+
240
+
241
+ self.w.setLayout(main)
242
+
243
+ self.setCentralWidget(self.w)
244
+
245
+
246
+
247
+ def getScale(self):
248
+
249
+ return self.graph.getScale()
250
+
251
+
252
+
253
+ def setScale(self):
254
+
255
+ self.x = int(self.box.text())
256
+
257
+ self.graph.setScale(self.x)
258
+
259
+ self.scene.setSceneRect(self.scene.itemsBoundingRect())
260
+
261
+ self.view.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
262
+
263
+
264
+
265
+
266
+
267
+ def main():
268
+
269
+ app = QApplication(sys.argv)
270
+
271
+ gui = MyWindow()
272
+
273
+ sys.exit(app.exec_())
274
+
275
+
276
+
277
+
278
+
279
+ if __name__ == '__main__':
280
+
281
+ main()
282
+
283
+ ```