PDA

View Full Version : Decide two items on top of each other,is there a z-index?



gully
16th March 2017, 18:27
I am using pyqt4 and I have drawn a line and a rectangle on.Now since they are on the same coordinate they overlap. I wanted to know if there is a z-index(used in css) sort of a thing which I can use to show or hide either the line on top of the triangle or vice versa.Also adding the code by how I have a line and rectangle.

from PyQt4 import QtGui
from PyQt4 import QtCore
import pyqtgraph as pg
import webcolors


class LineSegmentItem(pg.GraphicsObject):
def __init__(self, p1, p2,color):
pg.GraphicsObject.__init__(self)
self.p1 = p1
self.p2 = p2
self.color=color
self.generatePicture()

def generatePicture(self):
self.picture = QtGui.QPicture()
self.p = QtGui.QPainter(self.picture)
self.p.setPen(pg.mkPen(self.color))
self.p.drawLine(QtCore.QPoint(self.p1[0], self.p1[1]), QtCore.QPoint(self.p2[0], self.p2[1]))
self.p.end()

def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)

def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())

def modifyColor(self,color):
self.color=color
self.generatePicture()

def stretchLine(self,toSecondCoordinate):
self.p2[0]=toSecondCoordinate
self.update()

def getToSecondCoordinate(self):
return self.p2[0]


class CircleItem(pg.GraphicsObject):
def __init__(self, center, radius):
pg.GraphicsObject.__init__(self)
self.center = center
self.radius = radius
self.generatePicture()

def generatePicture(self):
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen('w'))
p.drawEllipse(self.center[0], self.center[1], self.radius * 2, self.radius * 2)
p.end()

def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)

def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())


class RectangleItem(pg.GraphicsObject):
def __init__(self, p1,p2,color,tickSize):
pg.GraphicsObject.__init__(self)
self.topLeft = p1
self.size = [abs(p2[0]-p1[0]),tickSize]
self.color=color
self.generatePicture()

def generatePicture(self):
self.picture = QtGui.QPicture()
p = QtGui.QPainter(self.picture)
p.setPen(pg.mkPen(self.color))

r,g,b=self.color
p.setBrush(QtGui.QColor(r,g,b))
tl = QtCore.QPointF(self.topLeft[0], self.topLeft[1])
size = QtCore.QSizeF(self.size[0], self.size[1])
p.drawRect(QtCore.QRectF(tl, size))
p.end()

def paint(self, p, *args):
p.drawPicture(0, 0, self.picture)

def boundingRect(self):
return QtCore.QRectF(self.picture.boundingRect())

def modifyColor(self,color):
self.color=color
self.generatePicture()

def stretchLine(self,toSecondCoordinate):
self.size[0]=toSecondCoordinate-self.topLeft[0]
self.update()

def getToSecondCoordinate(self):
return self.size[0]

12391

anda_skoa
17th March 2017, 09:17
Yes, see QGraphicsItem::zValue().

Cheers,
_