PyQtGraph的GraphicsLayout中的边距

时间:2014-11-23 17:46:00

标签: python pyqt pyqtgraph

使用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_() 

哪些输出:

enter image description here

我怎样才能摆脱绿色外线和窗口边界之间的小边距?

我可以做到这一点并使用l.setContentsMargins(-10., -10., -10., -10.),这有效:

enter image description here

但在我看来,这似乎是一个肮脏的伎俩,应该有另一个参数设置该保证金。这有可能吗?是否有另一个保证金参数,我可以将其设置为0以获得相同的结果?

1 个答案:

答案 0 :(得分:2)

我认为这可能是一个Qt错误。有一个简单的解决方法:

l = pg.GraphicsLayout()
l.layout.setContentsMargins(0, 0, 0, 0)

要理解这一点,让我们看一个修改过的例子:

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)

在此示例中,调用l.setContentsMargins(...)具有预期效果,但调用r1.setContentsMargins(...)则没有。 Qt文档表明效果应该是相同的,但是:http://qt-project.org/doc/qt-4.8/qgraphicswidget.html#setContentsMargins