回答編集履歴
2
サンプルコードの追加
test
CHANGED
@@ -49,3 +49,129 @@
|
|
49
49
|
通常のwidgetの場合
|
50
50
|
|
51
51
|
QPainter で画像を描画する場合は、drawImage や drawPixmap を使います。
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
----
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
![イメージ説明](335178bdfbe41a687cd387096148900e.gif)
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
出典: https://stackoverflow.com/questions/52728462/pyqt-add-rectangle-in-qgraphicsscene
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
サンプルコードを数カ所改変しました。
|
68
|
+
|
69
|
+
- 画像との重ね合わせを考慮して、透過色 QtGui.QColor(255, 0, 0, 128)
|
70
|
+
|
71
|
+
- 短形は一つのみ表示 self.removeItem(self._current_rect_item)
|
72
|
+
|
73
|
+
- 不要な設定の削除 ItemIsMovable フラグ
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
```python
|
78
|
+
|
79
|
+
from PyQt5 import QtCore, QtGui, QtWidgets
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
class GraphicsScene(QtWidgets.QGraphicsScene):
|
84
|
+
|
85
|
+
def __init__(self, parent=None):
|
86
|
+
|
87
|
+
super(GraphicsScene, self).__init__(QtCore.QRectF(-500, -500, 1000, 1000), parent)
|
88
|
+
|
89
|
+
self._start = QtCore.QPointF()
|
90
|
+
|
91
|
+
self._current_rect_item = None
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
def mousePressEvent(self, event):
|
96
|
+
|
97
|
+
if self._current_rect_item:
|
98
|
+
|
99
|
+
self.removeItem(self._current_rect_item)
|
100
|
+
|
101
|
+
if self.itemAt(event.scenePos(), QtGui.QTransform()) is None:
|
102
|
+
|
103
|
+
self._current_rect_item = QtWidgets.QGraphicsRectItem()
|
104
|
+
|
105
|
+
self._current_rect_item.setBrush(QtGui.QColor(255, 0, 0, 128))
|
106
|
+
|
107
|
+
# アイテムを移動可能にする -> すぐに削除する為不要
|
108
|
+
|
109
|
+
# self._current_rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
|
110
|
+
|
111
|
+
self.addItem(self._current_rect_item)
|
112
|
+
|
113
|
+
self._start = event.scenePos()
|
114
|
+
|
115
|
+
r = QtCore.QRectF(self._start, self._start)
|
116
|
+
|
117
|
+
self._current_rect_item.setRect(r)
|
118
|
+
|
119
|
+
super(GraphicsScene, self).mousePressEvent(event)
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
def mouseMoveEvent(self, event):
|
124
|
+
|
125
|
+
if self._current_rect_item is not None:
|
126
|
+
|
127
|
+
r = QtCore.QRectF(self._start, event.scenePos()).normalized()
|
128
|
+
|
129
|
+
self._current_rect_item.setRect(r)
|
130
|
+
|
131
|
+
super(GraphicsScene, self).mouseMoveEvent(event)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def mouseReleaseEvent(self, event):
|
136
|
+
|
137
|
+
# self._current_rect_item = None
|
138
|
+
|
139
|
+
super(GraphicsScene, self).mouseReleaseEvent(event)
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
class MainWindow(QtWidgets.QMainWindow):
|
146
|
+
|
147
|
+
def __init__(self, parent=None):
|
148
|
+
|
149
|
+
super(MainWindow, self).__init__(parent)
|
150
|
+
|
151
|
+
scene =GraphicsScene(self)
|
152
|
+
|
153
|
+
view = QtWidgets.QGraphicsView(scene)
|
154
|
+
|
155
|
+
self.setCentralWidget(view)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
if __name__ == '__main__':
|
162
|
+
|
163
|
+
import sys
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
app = QtWidgets.QApplication(sys.argv)
|
168
|
+
|
169
|
+
w = MainWindow()
|
170
|
+
|
171
|
+
w.resize(640, 480)
|
172
|
+
|
173
|
+
w.show()
|
174
|
+
|
175
|
+
sys.exit(app.exec_())
|
176
|
+
|
177
|
+
```
|
1
文章を微修正
test
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
マウスのイベントでアイテムの表示非表示/移動・リサイズを行うことで
|
38
38
|
|
39
|
-
選択範囲の枠
|
39
|
+
選択範囲の枠描画は実現できます。
|
40
40
|
|
41
41
|
|
42
42
|
|