如何将一个项目始终放在 PyQtGraph 中的其他项目之上?

How to place an item always over other items in PyQtGraph?

我想知道是否可以将某个项目放在其他项目之上,即使该项目比其他项目添加得早。

import pyqtgraph as pg
from pyqtgraph import QtCore, QtGui

class CandlestickItem(pg.GraphicsObject):
    def __init__(self, data):
        pg.GraphicsObject.__init__(self)
        self.data = data  
        self.generatePicture()
    
    def generatePicture(self):
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)
        p.setPen(pg.mkPen('w'))
        w = (self.data[1][0] - self.data[0][0]) / 3.
        for (t, open, close, min, max) in self.data:
            p.drawLine(QtCore.QPointF(t, min), QtCore.QPointF(t, max))
            if open > close:
                p.setBrush(pg.mkBrush('r'))
            else:
                p.setBrush(pg.mkBrush('g'))
            p.drawRect(QtCore.QRectF(t-w, open, w*2, close-open))
        p.end()
    
    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)
    
    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())

data = [  ## fields are (time, open, close, min, max).
    (1., 10, 13, 5, 15),
    (2., 13, 17, 9, 20),
    (3., 17, 14, 11, 23),
    (4., 14, 15, 5, 19),
    (5., 15, 9, 8, 22),
    (6., 9, 15, 8, 16),
]


plt = pg.plot()


sctItem = pg.ScatterPlotItem(symbol='s', pen=pg.mkPen(None), size=13, brush=(14, 40, 57, 255))
sctItem.setData( [1,2,3],[12,15,16] )
plt.addItem(sctItem)

candleItem = CandlestickItem(data)
plt.addItem(candleItem)



plt.setWindowTitle('pyqtgraph example: customGraphicsItem')

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

在上面的代码中,sctItem比candleItem先添加,看不到。有没有办法让一个项目保持在其他项目之上,之后是否添加新项目?

zValue 值决定兄弟姐妹(邻居)的堆叠顺序,因此在这种情况下,如果您希望 sctItem 位于其其他兄弟姐妹之上,则只需将其设置为 1,因为默认情况下它是0.

sctItem.setZValue(1)