QGraphicsRectItem继承自QGraphicsObject,无法将setRect和setGeometry设置为相同的QRect大小

时间:2017-06-30 16:19:00

标签: python pyqt pyqt4

QGraphicsRectItem继承自QGraphicsObject,无法将setRect和setGeometry设置为相同的QRect大小,

我在设置QGraphics对象的Geomtry,位置,宽度和高度与QGraphicsRectItem的大小时遇到​​了问题。

由于特定原因,我需要从QGraphicsObject继承我的QGraphicsRectItem(稍后我需要添加svg图像)

我的工作流程如下。

小部件 - > class Outer (父级是“小部件”) - > class InnerItem (parent是“Outer”) - > TextItem(parent是“InnerItem”)

  1. Outer 是红色矩形
  2. * InnerItem *为绿色矩形
  3. setRect大小“ Outer ”使用“childrenBoundingRect()”计算矩形 for“ InnerItem ”类是innerItem.childboundaryRect(),它应该是TextItem pos,width,height

    我需要绿色矩形框,在TextItem周围绘制,整个绿色矩形框(innerItem)应该是QObject,这是使用setGeomtry完成的。

    1. 当我点击时,绿色框的所有侧面,event.pos()返回 ,< main .InnerItem对象位于0x7f6123af6a68>,) (但绘图不在TextBox周围)
    2. 这是b'co​​s innerItem .childrenBoundingRect()也包含 QIraphicsRectitem的InnerItem大小,这可以通过手动重新计算childBoundingRect()

      来克服

      我尝试了很多选项,但是我无法将QRectItem和QObject Rectangle大小设置为相同的大小,如果我设置了那么event.pos()不会返回< main .InnerItem对象位于0x7f6123af6a68>它只返回 , 在RectItem的一个大小

      我在设置QGraphicsRectItem(setRect)和QGraphicsObject(setGeomtry)时做错了什么?

      至少逻辑上我正在尝试的是正确与否。

      我需要像第一张图片和第二张图片这样的东西:

      class GraphicalView(QtGui.QGraphicsView):
      
          def __init__(self, parent):
              QtGui.QGraphicsView.__init__(self,parent)
              self.setScene(parent)
      
          def mousePressEvent(self, event): 
              self.clickPosition  = self.mapToScene(event.pos())
              print "---------- Start ----------"
              print self.items(event.pos())
      
          def mouseMoveEvent(self,event):
              pass
      
          def mouseReleaseEvent(self, event):
      
              self.clickPosition  = self.mapToScene(event.pos())
              print "MRE ",self.items(event.pos())
              return
      
      class QGObj(QtGui.QGraphicsWidget):
          """Base class for display elemenets in kinetics layout"""
          defaultFontName = "Helvetica"
          defaultFontSize = 10
          name = "ITEM"
          def __init__(self, parent=None):
              QtGui.QGraphicsObject.__init__(self,parent)
              self.gobj = None
              self.pressed = False
              self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable,True)
              self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
              self.setAcceptHoverEvents(True)
      
          def getParentGraphicsObject(self):
              return self.parentItem()
      
          def paint(self, painter=None, option=None, widget = None):
              # painter.setPen(QtGui.QPen(QtGui.QPen(QtCore.Qt.black, 5,Qt.Qt.DashLine, Qt.Qt.RoundCap, Qt.Qt.RoundJoin)))
              # print " painter in QGrapobj ",self.boundingRect(),self.pos(),self.gobj.boundingRect()
              # painter.drawRect(self.pos().x(),self.pos().y(),self.boundingRect().width(),self.boundingRect().height()) 
              if self.hasFocus() or self.isSelected():
                  painter.setPen(QtGui.QPen(QtGui.QPen(QtCore.Qt.black, 1,Qt.Qt.SolidLine, Qt.Qt.RoundCap, Qt.Qt.RoundJoin)))
                  painter.drawRect(self.boundingRect())        
      
          def itemChange(self,change,value):
              if change == QtGui.QGraphicsItem.ItemSelectedChange and value == True:
                  self.emit(QtCore.SIGNAL("qgtextItemSelectedChange(PyQt_PyObject)"),self.mobj)
              return QtGui.QGraphicsItem.itemChange(self,change,value)
      
      
      class InnerItem(QGObj):
      
          def __init__(self,parent):
              QGObj.__init__(self,parent)
              self.gobj = QtGui.QGraphicsRectItem(self)
      
      
          def boundingRect(self):
              ''' reimplimenting boundingRect for redrawning '''
      
              return QtCore.QRectF(self.gobj.boundingRect())
              #return QtCore.QRectF(0,0,self.gobj.boundingRect().width(),self.gobj.boundingRect().height())
      
      class MainWindow(QtGui.QWidget):
      
          def __init__(self, parent = None):
              super(MainWindow, self).__init__(parent)
      
              ## setting up the scene
              self.scene = QtGui.QGraphicsScene(self)
              self.view = GraphicalView(self.scene)
              #self.scene.setSceneRect(0, 0, 500, 500)
      
              ## setting up MyRect
              outer = QtGui.QGraphicsRectItem()
              outer.setPen(QtGui.QPen(Qt.QColor("red"), 5, Qt.Qt.SolidLine, Qt.Qt.RoundCap, Qt.Qt.RoundJoin))
      
              ##inner RectItem, 
              ###child of Outer RectItem
              inner = InnerItem(outer)
      
              #textItem which is child of InnerItem class
              textItem  = QtGui.QGraphicsTextItem('My Text Here', inner)
              textItem.setDefaultTextColor(Qt.QColor("blue"))
              textItem.setPos(70,70)
      
      
              inner.gobj.setPen(QtGui.QPen(Qt.QColor("green"), 5, Qt.Qt.SolidLine, Qt.Qt.RoundCap, Qt.Qt.RoundJoin))
              innerCBR = inner.childrenBoundingRect()
              #inner.gobj.setRect(0,0,innerCBR.width(),innerCBR.height())
              # print " innerCBR ",innerCBR
              # print inner.childItems()
              # x,y = [],[]
              # for i in inner.childItems():
              #     if not isinstance(i,QtGui.QGraphicsRectItem):
              #         x.append((i.pos().x())+(i.boundingRect().bottomRight().x()))
              #         x.append(i.pos().x())
              #         y.append(i.pos().y()+i.boundingRect().bottomRight().y())
              #         y.append(i.pos().y())
              # innerCBR = QtCore.QRectF(min(x),min(y),max(x)-min(x),max(y)-min(y))
      
              inner.gobj.setRect(0,0,innerCBR.width(),innerCBR.height())
      
              inner.setGeometry(innerCBR.x(),innerCBR.y(),innerCBR.width(),innerCBR.height())
      
              outerCBR = inner.childrenBoundingRect()
              outer.setRect(outerCBR.x()-10,outerCBR.y()-10,outerCBR.width()+20,outerCBR.height()+20)
              self.scene.addItem(outer)
      
      
              layout = QtGui.QHBoxLayout()
              layout.addWidget(self.view)
              self.setLayout(layout) 
      
              self.setWindowTitle("QObj")
              #self.view.fitInView(self.scene.itemsBoundingRect().x()-10,self.scene.itemsBoundingRect().y()-10,self.scene.itemsBoundingRect().width()+20,self.scene.itemsBoundingRect().height()+20,Qt.Qt.IgnoreAspectRatio)
              self.view.show()
      def run():
          app = QtGui.QApplication(sys.argv)
          GUI = MainWindow()
          GUI.show()
          sys.exit(app.exec_())
      
      run()
      

      First image, this is how it should be

0 个答案:

没有答案