1

背景:我正在尝试使用 python 和 pyqt 制作网格工具。具体来说,我想使用 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


可能的解决方案:(我的随机想法)

  1. 在网格上实现一个不可见的矩形来处理交互
4

1 回答 1

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))
     ...
于 2013-04-04T14:46:19.843 回答