我想在网格中每个单元格的列中逐一添加图像。在这里,我创建了一个按钮。当我单击一个按钮时,一个图像将被添加到网格的单元格中。当我单击按钮时,我想将图像添加到网格的另一个单元格中。此过程将一直持续到网格中的单元格末尾。 我该如何使用pyqt4? 我目前的尝试如下:
import sys
from PyQt4 import QtCore, QtGui
class Setting:
WIDTH = 100
HEIGHT = 100
X, Y = 5, 3
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("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):
pass
class 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 :(得分:1)
使用drawBackground()
绘制永久物,对于图像,最好通过addPixmap()
将其添加到场景中,如下所示:
import sys
from PyQt4 import QtCore, QtGui
class QS(QtGui.QGraphicsScene):
def __init__(self, xcells, ycells, size, parent = None):
super(QS, self).__init__(QtCore.QRectF(0, 0, xcells*size.width(), ycells*size.height()), parent)
self._xcells = xcells
self._ycells = ycells
self._size_cell = size
def drawBackground(self, painter, rect):
width = self._xcells*self._size_cell.width()
height = self._ycells*self._size_cell.height()
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(width, 0))
for _ in range(self._ycells+1):
painter.drawLine(l)
l.translate(0, self._size_cell.height())
l = QtCore.QLineF(QtCore.QPointF(0, 0), QtCore.QPointF(0, height))
for _ in range(self._xcells+1):
painter.drawLine(l)
l.translate(self._size_cell.width(), 0)
class QV(QtGui.QGraphicsView):
def __init__(self, parent=None):
super(QV, self).__init__(parent)
self.size_cell = QtCore.QSize(100, 100)
self.xcells, self.ycells = 5, 3
scene = QS(self.xcells, self.ycells, self.size_cell, self)
self.setScene(scene)
self._i = 0
self._j = 0
def add_pixmap(self, pixmap):
if self._i*self._j < self.xcells*self.ycells:
it = self.scene().addPixmap(pixmap.scaled(self.size_cell, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation))
pos = QtCore.QPointF(self._j*self.size_cell.width(), self._i*self.size_cell.height())
it.setPos(pos)
self._i += 1
if self._i % self.ycells == 0:
self._j, self._i = self._j + 1, 0
class Example(QtGui.QWidget):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.view = QV()
btn = QtGui.QPushButton("measure")
btn.clicked.connect(self.on_clicked)
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(btn)
vbox.addWidget(self.view)
@QtCore.pyqtSlot()
def on_clicked(self):
filename = QtGui.QFileDialog.getOpenFileName(self,
"Open Image",
QtCore.QDir.currentPath(),
"Image Files (*.png *.jpg *.bmp)")
if filename:
pixmap = QtGui.QPixmap(filename)
if not pixmap.isNull():
self.view.add_pixmap(pixmap)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
如果只想加载相同的图像,则将on_clicked
替换为以下内容:
更新:
@QtCore.pyqtSlot()
def on_clicked(self):
filename = "path/of/image"
pixmap = QtGui.QPixmap(filename)
if not pixmap.isNull():
self.view.add_pixmap(pixmap)