回答編集履歴
3
あ
answer
CHANGED
@@ -164,4 +164,10 @@
|
|
164
164
|
|
165
165
|
日本語の情報源もそれなりにあります。
|
166
166
|
|
167
|
-
[Qiita](https://qiita.com/search?q=qt)
|
167
|
+
[Qiita](https://qiita.com/search?q=qt)
|
168
|
+
|
169
|
+
* 公式の Example
|
170
|
+
|
171
|
+
Qt Creator をインストールするとかなりの数の Example がついてきます。
|
172
|
+
|
173
|
+

|
2
あ
answer
CHANGED
@@ -139,4 +139,29 @@
|
|
139
139
|
|
140
140
|
if __name__ == '__main__':
|
141
141
|
main()
|
142
|
-
```
|
142
|
+
```
|
143
|
+
|
144
|
+
## 追記 Qt の情報源
|
145
|
+
|
146
|
+
C++ も PyQt もライブラリの使い方は同じで情報量は C++ のほうが多いので、検索するときは PyQt ではなく Qt で調べたほうがいいです。(C++ の文法を知らなくても大丈夫です。)
|
147
|
+
|
148
|
+
検索は日本語で出てこなければ、英語のキーワードで調べたほうがいいです。Stack Overflow でだいたい既出です。
|
149
|
+
|
150
|
+
* [Qt のリファレンス](http://doc.qt.io/qt-5/classes.html)
|
151
|
+
|
152
|
+
使用するクラスに関してだけ確認する。
|
153
|
+
|
154
|
+
* Stack Overflow / Qt Centre などの質問サイト
|
155
|
+
|
156
|
+
例えば、「QGraphicsView の表示スケールを QGraphicsScene がすべて収まるように変更したい」と思った場合は「qgraphicsview auto scale」と検索してみると、
|
157
|
+
|
158
|
+
1個目、2個目に答えが出てきます。
|
159
|
+
|
160
|
+
[Auto scale a QGraphicsView](https://www.qtcentre.org/threads/42917-Auto-scale-a-QGraphicsView)
|
161
|
+
[how to Fit QGraphicsScene in a QGraphicsView](https://stackoverflow.com/questions/10891962/how-to-fit-qgraphicsscene-in-a-qgraphicsview)
|
162
|
+
|
163
|
+
* Qiita などの日本語のサイト
|
164
|
+
|
165
|
+
日本語の情報源もそれなりにあります。
|
166
|
+
|
167
|
+
[Qiita](https://qiita.com/search?q=qt)
|
1
a
answer
CHANGED
@@ -30,4 +30,113 @@
|
|
30
30
|
また、右側で用意しているテキストボックスを使用して、円と円の間隔を変えるようにした場合、動的にスクロールバーの長さを変更するようなことはできるのでしょうか?
|
31
31
|
```
|
32
32
|
|
33
|
-
すべての円が表示されるように Example クラスの `def boundingRect(self)` が返す Rect を動的に変更する必要があります。
|
33
|
+
すべての円が表示されるように Example クラスの `def boundingRect(self)` が返す Rect を動的に変更する必要があります。
|
34
|
+
|
35
|
+
## 全部表示されるように変更したコード
|
36
|
+
|
37
|
+
* 原寸大で表示したい場合は fitInView() の2箇所をコメントアウトしてください。
|
38
|
+
* GGraphicsItem の大きさ等変更する場合 (scale の変更など) は変更前に prepareGeometryChange() をよんでください。変更が完了したら、update() で再描画すればよいです。
|
39
|
+
|
40
|
+
```python
|
41
|
+
import sys
|
42
|
+
from PyQt5.QtWidgets import *
|
43
|
+
from PyQt5.QtGui import *
|
44
|
+
from PyQt5.QtCore import *
|
45
|
+
|
46
|
+
|
47
|
+
class Example(QGraphicsItem):
|
48
|
+
def __init__(self, width=400, height=400):
|
49
|
+
super(Example, self).__init__()
|
50
|
+
|
51
|
+
self.width = width
|
52
|
+
self.height = height
|
53
|
+
self.x_origin = width / 2
|
54
|
+
self.y_origin = height / 2
|
55
|
+
self.scale = 20
|
56
|
+
|
57
|
+
def paint(self, painter, option, widget):
|
58
|
+
painter.setPen(Qt.black)
|
59
|
+
painter.drawLine(0, self.y_origin, self.width, self.y_origin)
|
60
|
+
painter.drawLine(self.x_origin, 0, self.x_origin, self.height)
|
61
|
+
print("paint")
|
62
|
+
|
63
|
+
for i in range(100):
|
64
|
+
x = self.x_origin + i * self.scale
|
65
|
+
y = self.y_origin + i * self.scale
|
66
|
+
painter.setPen(Qt.black)
|
67
|
+
painter.setBrush(QBrush(Qt.red, Qt.SolidPattern))
|
68
|
+
painter.drawEllipse(QPointF(x, y), 9, 9)
|
69
|
+
|
70
|
+
def boundingRect(self):
|
71
|
+
width = self.y_origin + 100 * self.scale + 30
|
72
|
+
height = self.y_origin + 100 * self.scale + 30
|
73
|
+
return QRectF(0, 0, width, height)
|
74
|
+
|
75
|
+
def setScale(self, x):
|
76
|
+
self.prepareGeometryChange()
|
77
|
+
self.scale = x
|
78
|
+
self.update()
|
79
|
+
|
80
|
+
def getScale(self):
|
81
|
+
return self.scale
|
82
|
+
|
83
|
+
|
84
|
+
class MyWindow(QMainWindow):
|
85
|
+
def __init__(self):
|
86
|
+
super().__init__()
|
87
|
+
self.initUI()
|
88
|
+
|
89
|
+
def initUI(self):
|
90
|
+
self.setGeometry(50, 50, 600, 500)
|
91
|
+
self.setWindowTitle("Ex")
|
92
|
+
self.setWindow()
|
93
|
+
self.show()
|
94
|
+
|
95
|
+
def setWindow(self):
|
96
|
+
self.w = QWidget()
|
97
|
+
self.view = QGraphicsView()
|
98
|
+
self.scene = QGraphicsScene(self.view)
|
99
|
+
self.srect = self.view.rect()
|
100
|
+
width = self.srect.width()
|
101
|
+
height = self.srect.height()
|
102
|
+
# self.scene.setSceneRect(QRectF(self.srect))
|
103
|
+
self.graph = Example(width, height)
|
104
|
+
self.scene.addItem(self.graph)
|
105
|
+
self.view.setScene(self.scene)
|
106
|
+
self.view.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
|
107
|
+
|
108
|
+
label = QLabel(" X ")
|
109
|
+
self.box = QLineEdit()
|
110
|
+
x = self.getScale()
|
111
|
+
self.box.setText("{}".format(x))
|
112
|
+
update = QPushButton('Update')
|
113
|
+
update.clicked.connect(self.setScale)
|
114
|
+
|
115
|
+
main = QGridLayout()
|
116
|
+
main.addWidget(self.view, 0, 0, 30, 5)
|
117
|
+
main.addWidget(label, 0, 10, 1, 3)
|
118
|
+
main.addWidget(self.box, 1, 10, 1, 3)
|
119
|
+
main.addWidget(update, 2, 10, 1, 3)
|
120
|
+
|
121
|
+
self.w.setLayout(main)
|
122
|
+
self.setCentralWidget(self.w)
|
123
|
+
|
124
|
+
def getScale(self):
|
125
|
+
return self.graph.getScale()
|
126
|
+
|
127
|
+
def setScale(self):
|
128
|
+
self.x = int(self.box.text())
|
129
|
+
self.graph.setScale(self.x)
|
130
|
+
self.scene.setSceneRect(self.scene.itemsBoundingRect())
|
131
|
+
self.view.fitInView(self.scene.sceneRect(), Qt.KeepAspectRatio)
|
132
|
+
|
133
|
+
|
134
|
+
def main():
|
135
|
+
app = QApplication(sys.argv)
|
136
|
+
gui = MyWindow()
|
137
|
+
sys.exit(app.exec_())
|
138
|
+
|
139
|
+
|
140
|
+
if __name__ == '__main__':
|
141
|
+
main()
|
142
|
+
```
|