Python 删除QGraphicsItem周围的虚线选择 我在网上搜索了一个解决方案,其中大部分都是C++的,没有完整的答案。我想知道是否有人可以帮我找到如何删除qgraphicsitem周围的选择矩形,如下图所示
导入系统 从PySide.QtGui导入* 从PySide.QtCore导入* 随机输入 类MyGraphicsView(QGraphicsView): 定义初始化(自): 超级(MyGraphicsView,self)。\uuuu init\uuuuu() self.setDragMode(QGraphicsView.RubberBandDrag) self.\u isPanning=False self.\u mousePressed=False #self.setBackgroundBrush(QImage(“C:/Users/jmartini/Desktop/Temp/images/flag_0140.jpg”)) self.setCacheMode(QGraphicsView.CacheBackground) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) def鼠标压力事件(自身、事件): 如果event.button()==Qt.LeftButton: self.\u mousePressed=True 如果自行规划: self.setCursor(Qt.ClosedHandCursor) self.\u dragPos=event.pos() event.accept() 其他: 超级(MyGraphicsView,self).mousePressEvent(事件) elif event.button()==Qt.MiddleButton: self.\u mousePressed=True self.\u isPanning=True self.setCursor(Qt.ClosedHandCursor) self.\u dragPos=event.pos() event.accept() def mouseMoveEvent(自身、事件): 如果按下self.\u鼠标并进行self.\u扫描: newPos=event.pos() diff=新位置-自 self.\u dragPos=newPos self.horizontalScrollBar().setValue(self.horizontalScrollBar().value()-diff.x()) self.verticalScrollBar().setValue(self.verticalScrollBar().value()-diff.y()) event.accept() 其他: 超级(MyGraphicsView,self).mouseMoveEvent(事件) def mouseReleaseEvent(自身,事件): 如果event.button()==Qt.LeftButton: 如果自行规划: self.setCursor(Qt.OpenHandCursor) 其他: self.\u isPanning=False self.setCursor(Qt.ArrowCursor) self.\u mousePressed=False elif event.button()==Qt.MiddleButton: self.\u isPanning=False self.setCursor(Qt.ArrowCursor) self.\u mousePressed=False 超级(MyGraphicsView,self).mouseReleaseEvent(事件) def mouseDoubleClickEvent(自身,事件): self.fitInView(self.scen直立(),Qt.KeepAspectRatio) 通过 def按键事件(自身,事件): 如果event.key()==Qt.key\u空格和非self.\u鼠标按下: self.\u isPanning=True self.setCursor(Qt.OpenHandCursor) 其他: 超级(MyGraphicsView,自我)。按键事件(事件) def钥匙释放事件(自身,事件): 如果event.key()==Qt.key\u空间: 如果不是self.\u鼠标按下: self.\u isPanning=False self.setCursor(Qt.ArrowCursor) 其他: 超级(MyGraphicsView,自我)。按键事件(事件) def wheelEvent(自我,事件): #缩放因子 系数=1.25 #锚定 self.setTransformationAnchor(QGraphicsView.NoAnchor) self.setResizeAnchor(QGraphicsView.NoAnchor) #保存场景pos oldPos=self.maptosene(event.pos()) #变焦 如果event.delta()小于0: 系数=1.0/系数 自我尺度(因子,因子) #获得新职位 newPos=self.maptosene(event.pos()) #将场景移动到旧位置 增量=新位置-旧位置 self.translate(delta.x(),delta.y()) 类别MyGraphicscene(QGraphicscene): 定义初始化(自身,父级): 超级(mygraphicscene,self)。\uuuu init\uuuuu() 自收根地刷(QBrush(QColor(50,50,50))) #自安装(50,50,0,0) 类MyMainWindow(QMainWindow): 定义初始化(自): 超级(MyMainWindow,self)。\uu初始化 self.setWindowTitle(“测试”) 自我调整大小(800600) self.gv=MyGraphicsView() 自gv设置场景(MyGraphicscene(自)) self.setCentralWidget(self.gv) self.gv.scene().selectionChanged.connect(self.selection\u已更改) self.populate() def填充(自我): scene=self.gv.scene() 对于范围(300)内的i: x=random.randint(-10001000) y=random.randint(-10001000) r=random.randint(1,50) rect=场景。加法(x,y,r,r, QPen(QColor(255128,0),0.5,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin), QBrush(QColor(255128,20128))) rect.setFlag(QGraphicsItem.itemisselect) rect.setFlag(QGraphicsItem.ItemIsMovable) rect=scene.addEllipse(10,20,20,20, QPen(QColor(255128,0),0.5,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin), QBrush(QColor(255,0,0128))) rect.setFlag(QGraphicsItem.itemisselect) rect.setFlag(QGraphicsItem.ItemIsMovable) 点=QPolygonF() 附加点(QPointF(10.4,10.5)) 附加点(QPointF(40.2,60.2)) 附加点(QPointF(30.2,90.2)) 附加点(QPointF(10.2,80.2)) 多边形=场景。添加多边形(点, QPen(QColor(255128,0),0.5,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin), QBrush(QColor(255,0,0128))) poly.setFlag(QGraphicsItem.itemisselect) poly.setFlag(QGraphicsItem.ItemIsMovable) def选择_已更改(自身): selection=self.gv.scene().selectedItems() 打印“选定:”,len(选定内容) 对于我的选择: i、 setPen(QPen(QColor(255255255),0.5,Qt.SolidLine,Qt.RoundCap,Qt.RoundJoin)) def main(): app=QApplication(sys.argv) ex=MyMainWindow() 例如:show() sys.exit(app.exec_())Python 删除QGraphicsItem周围的虚线选择 我在网上搜索了一个解决方案,其中大部分都是C++的,没有完整的答案。我想知道是否有人可以帮我找到如何删除qgraphicsitem周围的选择矩形,如下图所示,python,pyside,qgraphicsscene,qgraphicsitem,Python,Pyside,Qgraphicsscene,Qgraphicsitem,导入系统 从PySide.QtGui导入* 从PySide.QtCore导入* 随机输入 类MyGraphicsView(QGraphicsView): 定义初始化(自): 超级(MyGraphicsView,self)。\uuuu init\uuuuu() self.setDragMode(QGraphicsView.RubberBandDrag) self.\u isPanning=False self.\u mousePressed=False #self.setBackgroundBru
import sys
from PySide.QtGui import *
from PySide.QtCore import *
import random
class MyGraphicsView(QGraphicsView):
def __init__(self):
super(MyGraphicsView, self).__init__()
self.setDragMode(QGraphicsView.RubberBandDrag)
self._isPanning = False
self._mousePressed = False
# self.setBackgroundBrush(QImage("C:/Users/jmartini/Desktop/Temp/images/flag_0140.jpg"))
self.setCacheMode(QGraphicsView.CacheBackground)
self.setHorizontalScrollBarPolicy( Qt.ScrollBarAlwaysOff )
self.setVerticalScrollBarPolicy( Qt.ScrollBarAlwaysOff )
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self._mousePressed = True
if self._isPanning:
self.setCursor(Qt.ClosedHandCursor)
self._dragPos = event.pos()
event.accept()
else:
super(MyGraphicsView, self).mousePressEvent(event)
elif event.button() == Qt.MiddleButton:
self._mousePressed = True
self._isPanning = True
self.setCursor(Qt.ClosedHandCursor)
self._dragPos = event.pos()
event.accept()
def mouseMoveEvent(self, event):
if self._mousePressed and self._isPanning:
newPos = event.pos()
diff = newPos - self._dragPos
self._dragPos = newPos
self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - diff.x())
self.verticalScrollBar().setValue(self.verticalScrollBar().value() - diff.y())
event.accept()
else:
super(MyGraphicsView, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
if self._isPanning:
self.setCursor(Qt.OpenHandCursor)
else:
self._isPanning = False
self.setCursor(Qt.ArrowCursor)
self._mousePressed = False
elif event.button() == Qt.MiddleButton:
self._isPanning = False
self.setCursor(Qt.ArrowCursor)
self._mousePressed = False
super(MyGraphicsView, self).mouseReleaseEvent(event)
def mouseDoubleClickEvent(self, event):
self.fitInView(self.sceneRect(), Qt.KeepAspectRatio)
pass
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space and not self._mousePressed:
self._isPanning = True
self.setCursor(Qt.OpenHandCursor)
else:
super(MyGraphicsView, self).keyPressEvent(event)
def keyReleaseEvent(self, event):
if event.key() == Qt.Key_Space:
if not self._mousePressed:
self._isPanning = False
self.setCursor(Qt.ArrowCursor)
else:
super(MyGraphicsView, self).keyPressEvent(event)
def wheelEvent(self, event):
# zoom factor
factor = 1.25
# Set Anchors
self.setTransformationAnchor(QGraphicsView.NoAnchor)
self.setResizeAnchor(QGraphicsView.NoAnchor)
# Save the scene pos
oldPos = self.mapToScene(event.pos())
# Zoom
if event.delta() < 0:
factor = 1.0 / factor
self.scale(factor, factor)
# Get the new position
newPos = self.mapToScene(event.pos())
# Move scene to old position
delta = newPos - oldPos
self.translate(delta.x(), delta.y())
class MyGraphicsScene(QGraphicsScene):
def __init__(self, parent):
super(MyGraphicsScene, self).__init__()
self.setBackgroundBrush(QBrush(QColor(50,50,50)))
# self.setSceneRect(50,50,0,0)
class MyMainWindow(QMainWindow):
def __init__(self):
super(MyMainWindow, self).__init__()
self.setWindowTitle("Test")
self.resize(800,600)
self.gv = MyGraphicsView()
self.gv.setScene(MyGraphicsScene(self))
self.setCentralWidget(self.gv)
self.gv.scene().selectionChanged.connect(self.selection_changed)
self.populate()
def populate(self):
scene = self.gv.scene()
for i in range(300):
x = random.randint(-1000, 1000)
y = random.randint(-1000, 1000)
r = random.randint(1, 50)
rect = scene.addEllipse(x, y, r, r,
QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QBrush(QColor(255,128,20,128)))
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
rect = scene.addEllipse(10, 20, 20, 20,
QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QBrush(QColor(255,0,0,128)))
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
points = QPolygonF()
points.append(QPointF(10.4, 10.5))
points.append(QPointF(40.2, 60.2))
points.append(QPointF(30.2, 90.2))
points.append(QPointF(10.2, 80.2))
poly = scene.addPolygon(points,
QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin),
QBrush(QColor(255,0,0,128)))
poly.setFlag( QGraphicsItem.ItemIsSelectable )
poly.setFlag( QGraphicsItem.ItemIsMovable )
def selection_changed(self):
selection = self.gv.scene().selectedItems()
print 'Selected:', len(selection)
for i in selection:
i.setPen(QPen(QColor(255,255,255), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
def main():
app = QApplication(sys.argv)
ex = MyMainWindow()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
class EllipseItem(QGraphicsEllipseItem):
def paint(self, painter, option, widget):
option.state &= ~QStyle.State_Selected
super(EllipseItem, self).paint(painter, option, widget)
class PolygonItem(QGraphicsPolygonItem):
def paint(self, painter, option, widget):
option.state &= ~QStyle.State_Selected
super(PolygonItem, self).paint(painter, option, widget)
def populate(self):
scene = self.gv.scene()
for i in range(300):
x = random.randint(-1000, 1000)
y = random.randint(-1000, 1000)
r = random.randint(1, 50)
rect = EllipseItem(x, y, r, r)
rect.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
rect.setBrush(QBrush(QColor(255,128,20,128)))
scene.addItem(rect)
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
rect = EllipseItem(10, 20, 20, 20)
rect.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
rect.setBrush(QBrush(QColor(255,0,0,128)))
rect.setFlag( QGraphicsItem.ItemIsSelectable )
rect.setFlag( QGraphicsItem.ItemIsMovable )
scene.addItem(rect)
points = QPolygonF()
points.append(QPointF(10.4, 10.5))
points.append(QPointF(40.2, 60.2))
points.append(QPointF(30.2, 90.2))
points.append(QPointF(10.2, 80.2))
poly = PolygonItem(points)
poly.setPen(QPen(QColor(255,128,0), 0.5, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
poly.setBrush(QBrush(QColor(255,0,0,128)))
poly.setFlag( QGraphicsItem.ItemIsSelectable )
poly.setFlag( QGraphicsItem.ItemIsMovable )
scene.addItem(poly)