Search code examples
pythongridpyqt4cell

how to add the image one by one into each cell of the grid by using pushbutton


I want to add the image one by one in columns wise of the each cell in grid. Here, I created a push button. When I click a push button, one image will be added to cell of the grid. When I click the push button, I want to add an image into one more cell of the grid.. This process will continue up to end of cells in the grid. How can I do this with pyqt4? My current attempt is below:

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_())

Solution

  • Use drawBackground() to paint the permanent, in the case of the images it is better to add it to the scene through addPixmap() as I show below:

    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_())
    

    If you only want to load the same image then replace on_clicked with the following:

    Update:

    @QtCore.pyqtSlot()
    def on_clicked(self):
        filename = "path/of/image"
        pixmap = QtGui.QPixmap(filename)
        if not pixmap.isNull():
            self.view.add_pixmap(pixmap)