在这里,我在Q Graphics Scene类中创建了一个网格,我想在该网格的每个单元格中添加一个图像,所以请您帮我。如何在网格的每个单元格中添加图像
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)
答案 0 :(得分:1)
您的问题可以用多种方式解释,因此我将展示几个选项:
1。。如果您不想与这些项目进行交互,例如不想移动它,选择它,旋转它等,那么就不需要使用该项目,在这种情况下,最好的方法是使用drawBackground()
,因为一件物品比简单的绘画要消耗更多的资源。
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。。如果要与图像进行交互,可以使用QGraphicsPixmapItem
或两者的组合:
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_())