From 0e7b8a4828606049ce94669c4ef02fed7da04e9c Mon Sep 17 00:00:00 2001 From: Anthony De Bortoli Date: Sun, 11 Apr 2021 11:40:11 +0200 Subject: [PATCH] GridItem: Add custom line width based on pen option. --- examples/GraphicsScene.py | 17 +++++++++++------ pyqtgraph/graphicsItems/GridItem.py | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/examples/GraphicsScene.py b/examples/GraphicsScene.py index 3caf87c6..39d3dc3b 100644 --- a/examples/GraphicsScene.py +++ b/examples/GraphicsScene.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- ## Add path to library (just for examples; you do not need this) +from PySide2.QtGui import QPen, QColor + import initExample from pyqtgraph.Qt import QtCore, QtGui @@ -15,27 +17,27 @@ class Obj(QtGui.QGraphicsObject): def __init__(self): QtGui.QGraphicsObject.__init__(self) GraphicsScene.registerObject(self) - + def paint(self, p, *args): p.setPen(pg.mkPen(200,200,200)) p.drawRect(self.boundingRect()) - + def boundingRect(self): return QtCore.QRectF(0, 0, 20, 20) - + def mouseClickEvent(self, ev): if ev.double(): print("double click") else: print("click") ev.accept() - + #def mouseDragEvent(self, ev): #print "drag" #ev.accept() #self.setPos(self.pos() + ev.pos()-ev.lastPos()) - - + + vb = pg.ViewBox() win.setCentralItem(vb) @@ -56,6 +58,9 @@ prox.setPos(100,0) vb.addItem(prox) g = pg.GridItem() +p = QPen(QColor(0, 255, 255)) +p.setWidthF(1) +g.opts["pen"] = p vb.addItem(g) if __name__ == '__main__': diff --git a/pyqtgraph/graphicsItems/GridItem.py b/pyqtgraph/graphicsItems/GridItem.py index 4cb30bf8..2215e53a 100644 --- a/pyqtgraph/graphicsItems/GridItem.py +++ b/pyqtgraph/graphicsItems/GridItem.py @@ -9,7 +9,7 @@ __all__ = ['GridItem'] class GridItem(UIGraphicsItem): """ **Bases:** :class:`UIGraphicsItem ` - + Displays a rectangular grid of lines indicating major divisions within a coordinate system. Automatically determines what divisions to use. """ @@ -87,7 +87,7 @@ class GridItem(UIGraphicsItem): self.picture = None #UIGraphicsItem.viewRangeChanged(self) #self.update() - + def paint(self, p, opt, widget): #p.setPen(QtGui.QPen(QtGui.QColor(100, 100, 100))) #p.drawRect(self.boundingRect()) @@ -101,22 +101,22 @@ class GridItem(UIGraphicsItem): #p.drawLine(0, -100, 0, 100) #p.drawLine(-100, 0, 100, 0) #print "drawing Grid." - - + + def generatePicture(self): self.picture = QtGui.QPicture() p = QtGui.QPainter() p.begin(self.picture) - + vr = self.getViewWidget().rect() unit = self.pixelWidth(), self.pixelHeight() dim = [vr.width(), vr.height()] lvr = self.boundingRect() ul = np.array([lvr.left(), lvr.top()]) br = np.array([lvr.right(), lvr.bottom()]) - + texts = [] - + if ul[1] > br[1]: x = ul[1] ul[1] = br[1] @@ -157,6 +157,7 @@ class GridItem(UIGraphicsItem): linePen = self.opts['pen'] lineColor = self.opts['pen'].color() + lineWidth = self.opts['pen'].width() lineColor.setAlpha(c) linePen.setColor(lineColor) @@ -170,9 +171,9 @@ class GridItem(UIGraphicsItem): for x in range(0, int(nl[ax])): linePen.setCosmetic(False) if ax == 0: - linePen.setWidthF(self.pixelWidth()) + linePen.setWidthF(lineWidth * self.pixelWidth()) else: - linePen.setWidthF(self.pixelHeight()) + linePen.setWidthF(lineWidth * self.pixelHeight()) p.setPen(linePen) p1 = np.array([0.,0.]) p2 = np.array([0.,0.])