【问题标题】:PyQt grouping graphicsScene objects into one objectPyQt 将 graphicsScene 对象分组为一个对象
【发布时间】:2013-04-04 12:45:57
【问题描述】:

背景:我正在尝试使用 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. 在网格顶部实现一个不可见的矩形来处理交互

【问题讨论】:

    标签: python qt pyqt pyqt4


    【解决方案1】:

    另一种方法是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))
         ...
    

    【讨论】:

    • 不错。我没有意识到有一个 QGraphicsItemGroup。我得试试看。
    猜你喜欢
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多