import sys
if __name__ == '__main__':
# A background image
gradient.
setColorAt(0,
QColor( 0,
0,
255)) gradient.
setColorAt(1,
QColor(255,
255,
255)) painter.setBrush(gradient)
painter.
setPen(QColor(255,
0,
0)) painter.drawRect(0, 0, 250, 250)
transform = QTransform()
transform.translate(-125, -125)
# Try it without, and then with, the following line
#brush.setTransform(transform)
s.setBackgroundBrush(brush)
# small reference circle around the origin
c = s.addEllipse(-20, -20, 40, 40)
w.setSceneRect(-550, -550, 1100, 1100);
w.show()
sys.exit(app.exec_())
import sys
from PyQt5.QtGui import QImage, QPainter, QColor, QBrush, QLinearGradient, QTransform
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsView
if __name__ == '__main__':
app = QApplication(sys.argv)
# A background image
bgImage = QImage(250, 250, QImage.Format_ARGB32)
painter = QPainter(bgImage)
gradient = QLinearGradient(0, 0, 250, 0)
gradient.setColorAt(0, QColor( 0, 0, 255))
gradient.setColorAt(1, QColor(255, 255, 255))
painter.setBrush(gradient)
painter.setPen(QColor(255, 0, 0))
painter.drawRect(0, 0, 250, 250)
brush = QBrush(bgImage)
transform = QTransform()
transform.translate(-125, -125)
# Try it without, and then with, the following line
#brush.setTransform(transform)
s = QGraphicsScene()
s.setBackgroundBrush(brush)
# small reference circle around the origin
c = s.addEllipse(-20, -20, 40, 40)
w = QGraphicsView(s)
w.setSceneRect(-550, -550, 1100, 1100);
w.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks