GridItem: Add custom line width based on pen option.

This commit is contained in:
Anthony De Bortoli 2021-04-11 11:40:11 +02:00
parent 5a08650853
commit 0e7b8a4828
2 changed files with 21 additions and 15 deletions

View File

@ -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__':

View File

@ -9,7 +9,7 @@ __all__ = ['GridItem']
class GridItem(UIGraphicsItem):
"""
**Bases:** :class:`UIGraphicsItem <pyqtgraph.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.])