Reputation: 428
Here i created a grid in the Q Graphics Scene class and i want to add a image in to each cell in the grid, so can you please help me. how can i add the image in each cell of the grid
class QS(QtGui.QGraphicsScene):
def __init__(self, *args, **kwargs):
super(QS, self).__init__(*args, **kwargs)
# self.grview = QtGui.QGraphicsView()
self.scene = QtGui.QGraphicsScene()
self.scene.addPixmap(QtGui.QPixmap('pradeep.jpg'))
self.grview.setScene(self.scene)
print "Inserted in QS col = {}, row = {}".format(X,Y)
width = X * Setting.WIDTH
height = Y * Setting.HEIGHT
self.setSceneRect(0, 0, width, height)
self.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
for x in range(0,X+1):
xc = x * Setting.WIDTH
self.addLine(xc,0,xc,height)
self.scene.addPixmap(QtGui.QPixmap('pradeep.jpg'))
# self.addPixmap(self.pixmap)
for y in range(0,Y+1):
yc = y * Setting.HEIGHT
self.addLine(0,yc,width,yc)
self.scene.addPixmap(QtGui.QPixmap('pradeep.jpg'))
# self.addPixmap(self.pixmap)
# for x in range(0,X+1):
# for y in range(0,Y+1):
# self.addPixmap(0,y,imag)
class QV(QtGui.QGraphicsView, QtGui.QMainWindow):
def __init__(self, *args, **kwargs):
super(QV, self).__init__(*args, **kwargs)
Upvotes: 1
Views: 1566
Reputation: 244003
Your question can be interpreted in many ways so I will show several options:
1. If you are not going to interact with the items as for example you do not want to move it, select it, rotate it, etc. then it is not necessary to use an item, the best in that case is to use drawBackground()
since an item consumes more resources than a simple painted.
import sys
from PyQt4 import QtCore, QtGui
class Setting:
WIDTH = 80
HEIGHT = 80
X, Y = 7, 5
class QS(QtGui.QGraphicsScene):
def __init__(self, parent=None):
super(QS, self).__init__(QtCore.QRectF(0, 0, X * Setting.WIDTH, Y * Setting.HEIGHT), parent)
def drawBackground(self, painter, rect):
width = X * Setting.WIDTH
height = Y * Setting.HEIGHT
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
for _ in range(Y+1):
painter.drawLine(l)
l.translate(0, Setting.HEIGHT)
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
for _ in range(X+1):
painter.drawLine(l)
l.translate(Setting.WIDTH, 0)
pixmap = QtGui.QPixmap("pradeep.png").scaled(Setting.WIDTH,
Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation)
p = QtCore.QPointF()
for i in range(X):
p = QtCore.QPointF(Setting.WIDTH*i, 0)
for j in range(Y):
painter.drawPixmap(p, pixmap)
p += QtCore.QPointF(0, Setting.HEIGHT)
class QV(QtGui.QGraphicsView):
pass
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QS(self)
view = QV(scene)
self.setCentralWidget(view)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
2. If you want to interact with the images you could use QGraphicsPixmapItem
, or a combination of both:
import sys
from PyQt4 import QtCore, QtGui
class Setting:
WIDTH = 80
HEIGHT = 80
X, Y = 7, 5
class QS(QtGui.QGraphicsScene):
def __init__(self, parent=None):
super(QS, self).__init__(QtCore.QRectF(0, 0, X * Setting.WIDTH, Y * Setting.HEIGHT), parent)
pixmap = QtGui.QPixmap("pradeep.png").scaled(Setting.WIDTH,
Setting.HEIGHT,
QtCore.Qt.IgnoreAspectRatio,
QtCore.Qt.SmoothTransformation)
p = QtCore.QPointF()
for i in range(X):
p = QtCore.QPointF(Setting.WIDTH*i, 0)
for j in range(Y):
it = self.addPixmap(pixmap)
it.setPos(p)
p += QtCore.QPointF(0, Setting.HEIGHT)
def drawBackground(self, painter, rect):
width = X * Setting.WIDTH
height = Y * Setting.HEIGHT
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
for _ in range(Y+1):
painter.drawLine(l)
l.translate(0, Setting.HEIGHT)
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
for _ in range(X+1):
painter.drawLine(l)
l.translate(Setting.WIDTH, 0)
class QV(QtGui.QGraphicsView):
pass
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
scene = QS(self)
view = QV(scene)
self.setCentralWidget(view)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Upvotes: 1