我想在网格中每个单元格的列中一一添加图像。在这里,我创建了一个按钮。当我点击一个按钮时,一个图像将被添加到网格的单元格中。当我单击按钮时,我想将图像添加到网格的另一个单元格中。此过程将一直持续到网格中的单元格末尾。我怎样才能用 pyqt4 做到这一点?我目前的尝试如下:import sysfrom PyQt4 import QtCore, QtGuiclass Setting: WIDTH = 100 HEIGHT = 100X, Y = 5, 3class 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("image.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): passclass Example(QtGui.QWidget): def __init__(self, parent=None): super(Example, self).__init__(parent) scene = QS(self) view = QV(scene) self.vbox=QtGui.QVBoxLayout() self.btn=QtGui.QPushButton("measure") # self.btn.clicked.connect(self.showimage) self.vbox.addWidget(self.btn) self.vbox.addWidget(view) self.setLayout(self.vbox) # self.setCentralWidget(view)if __name__ == "__main__": app = QtGui.QApplication(sys.argv) w = Example() w.show() sys.exit(app.exec_())
添加回答
举报
0/150
提交
取消