Issue
Having a simple graphics layout with PyQtGraph:
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
app = QtGui.QApplication([])
view = pg.GraphicsView()
l = pg.GraphicsLayout(border='g')
view.setCentralItem(l)
view.show()
view.resize(800,600)
l.addPlot(0, 0)
l.addPlot(1, 0)
l.layout.setSpacing(0.)
l.setContentsMargins(0., 0., 0., 0.)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
Whichs outputs:
How could I get rid of the small margins which are between the green external line and the window borders?
I could do the trick and use l.setContentsMargins(-10., -10., -10., -10.)
, and that works:
But it seems to me like a dirty trick and there should be another parameter which is setting that margin. Could this be possible? Is there another margin parameter which I could set to 0
to get the same results?
Solution
I think this might be a Qt bug. There's an easy workaround:
l = pg.GraphicsLayout()
l.layout.setContentsMargins(0, 0, 0, 0)
To understand this, let's look at a modified example:
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
app = QtGui.QApplication([])
view = pg.GraphicsView()
view.show()
view.resize(800,600)
class R(QtGui.QGraphicsWidget):
# simple graphics widget that draws a rectangle around its geometry
def boundingRect(self):
return self.mapRectFromParent(self.geometry()).normalized()
def paint(self, p, *args):
p.setPen(pg.mkPen('y'))
p.drawRect(self.boundingRect())
l = QtGui.QGraphicsGridLayout()
r1 = R()
r2 = R()
r3 = R()
r1.setLayout(l)
l.addItem(r2, 0, 0)
l.addItem(r3, 1, 0)
view.scene().addItem(r1)
In this example, calling l.setContentsMargins(...)
has the expected effect, but calling r1.setContentsMargins(...)
does not. The Qt docs suggest that the effect should have been the same, though: http://qt-project.org/doc/qt-4.8/qgraphicswidget.html#setContentsMargins
Answered By - Luke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.