QGraphicsRectItem, gridlines and zooming

89 views
Skip to first unread message

Stewart Holmes

unread,
Jul 12, 2016, 8:51:45 AM7/12/16
to pyqtgraph
Hi all,

See the below working example. I am trying to put a QGraphicsRectItem on a figure (eventually to be some polygons, but I kept it simple here). I want the axis gridlines to be painted on top of this rectangle, however when I do this the zooming with mouse functionality stops working. It seems there may be something with the z order and what item is receiving the mouse events/how they are handled?

Regards,
Stewart


from PyQt4 import QtCore, QtGui
import sys
import numpy as np
import pyqtgraph as pg

pg
.setConfigOption('background', 'w'); pg.setConfigOption('foreground', 'k')


a
= QtGui.QApplication([])


p
= pg.PlotWidget()
line
= p.plot( x = np.arange(400, 600), y = np.random.rand(200), pen = pg.mkPen('r') )
p
.setXRange(0, 1000)
p
.setYRange(0, 1)
p
.plotItem.getAxis('left').setGrid(255)
p
.plotItem.getAxis('bottom').setGrid(255)


rect
= QtGui.QGraphicsRectItem( QtCore.QRectF( QtCore.QPointF(0, 0), QtCore.QSizeF(1000, 1.0) ) )
rect
.setPen( pg.mkPen('b', width = 2) )
rect.setBrush( pg.mkBrush( ( 220, 220, 220 ) ) )
p
.plotItem.addItem(rect)


rect
.setZValue( line.zValue() - 1)


# By default, mouse-wheel zoom works in both x and y directions, but grid is hidden below rect


# When uncommenting the below lines:
# Mouse-wheel zoom works when the mouse is over the x & y axis labels, but when it is
# over the plot itself, zoom is only in the y direction
p
.plotItem.getAxis('bottom').setZValue( rect.zValue() + 1 )
p
.plotItem.getAxis('left').setZValue( rect.zValue() + 1 )
p
.plotItem.getAxis('right').setZValue( rect.zValue() + 1 )
p
.plotItem.getAxis('top').setZValue( rect.zValue() + 1 )


p
.show()
sys
.exit(a.exec_())



Reply all
Reply to author
Forward
0 new messages