背景:我正在尝试使用python和pyqt创建一个griding工具。具体来说,我想使用graphicsView / graphicsScene组合,允许用户放置和编辑网格部分。在这种情况下,网格只是垂直线的集合。
类似的东西:
问题:如何将一组线条组合成一个对象,以便线条集合充当单个对象(即上下文菜单,拖动等)
当前代码:(仅限图形类)
from PyQt4 import QtGui, QtCore
class graphicsView(QtGui.QGraphicsView):
def __init__(self,parent=None):
super(graphicsView, self).__init__(parent)
self.parent=parent
def wheelEvent(self,event):
super(graphicsView, self).wheelEvent(event)
factor = 1.2
if event.delta()<0:
factor = 1.0/factor
self.scale(factor,factor)
class graphicsScene(QtGui.QGraphicsScene):
def __init__(self,parent=None):
super(graphicsScene, self).__init__(parent)
self.meshPen=QtGui.QPen(QtCOre.Qt.blue, 1, QtCore.Qt.SolidLine)
def newGrid(self, xmax,ymax,xcells,ycells,xmin=0,ymin=0):
for i in range(xcells+1):
x=i*(xmax-xmin)/xcells-abs(xmin)
self.addLine(x,ymin,x,ymax,self.meshPen)
for j in range(ycells+1):
y=j*(ymax-ymin)/ycells-abs(ymin)
self.addLine(xmin,y,xmax,y,self.meshPen)
系统
Python 2.7.2,PyQt4 4.9.5-2,windows 7
可能的解决方案:(我随意的想法)
答案 0 :(得分:2)
另一种方法是QGraphicsItemGroup:
def newGrid(...):
group = QtGui.QGraphicsItemGroup(scene=self)
group.setFlag(QtGui.QGraphicsItem.ItemIsMovable) #let't test how it works
for i in range(xccells + 1):
...
group.addToGroup(self.addLine(x,ymin,x,ymax,self.meshPen))
...