2013-04-04 33 views
1

背景:我正在尝试使用python和pyqt制作一个夹持工具。具体而言,我想使用graphicsView/graphicsScene组合来允许用户放置和编辑网格部分。在这种情况下,网格只是一组垂直线。PyQt将graphicsScene对象分组到一个对象中

类似:
enter image description here

问:我如何组行的集合在一起成为一个单一的对象,以便线的集合作为一个单独的对象(即上下文菜单,拖动等。)

当前代码:(只有图形类)

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的


可能的解决方案:(我的杂感)

  1. 实现一个看不见的矩形ontop的的网格来处理交互

回答

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)) 
    ... 
+0

不错。我没有意识到有一个QGraphicsItemGroup。我将不得不尝试一下。 – Onlyjus 2013-04-04 15:01:38