I'm using the following in the init of my QtGui.QGraphicsView
to make a nice grid/cross pattern. But not sure how to change the background color or thickness of the cross pattern lines? Using setColor
to set the color, but this only changes the color of the crossPattern and not the background.
Is there a way to change these, or should I be using a different type of style?
import PySide.QtGui as QtGui
import PySide.QtCore as QtCore
class NodeGraphView(QtGui.QGraphicsView):
def __init__(self, parent):
super(NodeGraphView, self).__init__(parent)
self.fg_brush = QtGui.QBrush()
self.fg_brush.setStyle(QtCore.Qt.CrossPattern)
self.fg_brush.setColor(QtGui.QColor(42, 42, 42, 255))
self.setBackgroundBrush(self.fg_brush)
The view background is basically just for the "fill"; the cross pattern is very basic and not configurable (other than color, because that's basic fill property). But it is not difficult to draw your own grid, and then you have much more control (such as thickness, dotted/dashed, showing origin, etc):
Example:
If
setCosmetic(True)
is not issued, the line thickness will increase as you zoom in.The nice thing with the above is that the lines are at a fixed coordinate in the scene. However, if you zoom out, you may have to add more lines, or make the existing lines longer. You could do this by overriding the scene's
drawBackground()
, which gets called with the rect of the scene that is in view: there you adjust the line endpoints.