Search code examples
pythonpython-2.7pyqtpyqt4

how to get the selected last clickable image in verticalbox layout


I want display my clickable image in vertical layout.i got similar example from google but i don't know how to get the image from clickable label. So can anyone please help me to add the image in vbox if i selected another items then the first item will remove in the vertical box.i want to display the last clickable item only. Here i tried so many ways but i am getting the object.Thank you in advance. Given below is the example:

from PyQt4 import QtCore, QtGui
import functools

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        highlight_dir = './floder1'
        scrollArea = QtGui.QScrollArea(widgetResizable=True)
        self.setCentralWidget(scrollArea)
        content_widget = QtGui.QWidget()
        scrollArea.setWidget(content_widget)
        self._layout = QtGui.QGridLayout(content_widget)
        self._it = QtCore.QDirIterator(highlight_dir)
        self.vbox = QtGui.QVBoxLayout()
        self.mainLayout = QtGui.QGridLayout()
        self.mainLayout.addLayout(self.vbox,0,1)
        self.mainLayout.addWidget(scrollArea,0,0)
        self.setCentralWidget(QtGui.QWidget(self))
        self.centralWidget().setLayout(self.mainLayout)
        self._row, self._col = 0, 0

        QtCore.QTimer(self, interval=1, timeout=self.load_image).start()


    def load_image(self):
        if self._it.hasNext():
            pixmap = QtGui.QPixmap(self._it.next())
            if not pixmap.isNull():
                vlay = QtGui.QVBoxLayout()
                self.label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
                self.label_pixmap.mousePressEvent= functools.partial(self.item_discription,source_object=self.label_pixmap)
                self.label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
                print self.label_text.text()
                vlay.addWidget(self.label_pixmap)
                vlay.addWidget(self.label_text)
                vlay.addStretch()
                self._layout.addLayout(vlay, self._row, self._col)
                self._col += 1
                if self._col == 3:
                    self._row += 1
                    self._col = 0
    def item_discription(self,event,source_object=None):
            print self.label_text.text() #how to add clickable qlabel to vbox
            self.vbox.addwidget(label_pixmap)



if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.setGeometry(500, 300, 900, 500)
    w.show()
    sys.exit(app.exec_())

Solution

  • If you want to make a QLabel notify you when there is a click, the simplest thing is to write it as indicated in this answer.

    On the other hand it is better that you use a QScrollArea as the basis of the QVBoxLayout since having a lot will not be able to visualize them all.

    Finally I prefer to use QSplitter since it allows to change the size of the widgets easily.

    from PyQt4 import QtCore, QtGui
    from functools import partial
    
    class ClickableLabel(QtGui.QLabel):
        clicked = QtCore.pyqtSignal()
    
        def mousePressEvent(self, event):
            self.clicked.emit()
            super(ClickableLabel, self).mousePressEvent(event)
    
    class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
            content_widget_left = QtGui.QWidget()
            scrollArea_left.setWidget(content_widget_left)
            self._layout = QtGui.QGridLayout(content_widget_left)
    
            scrollArea_right = QtGui.QScrollArea(widgetResizable=True)
            content_widget_right = QtGui.QWidget()
            scrollArea_right.setWidget(content_widget_right)
            self._vbox = QtGui.QVBoxLayout(content_widget_right)
    
            splitter = QtGui.QSplitter()
            splitter.addWidget(scrollArea_left)
            splitter.addWidget(scrollArea_right)
            splitter.setStretchFactor(0, 3)
            splitter.setStretchFactor(1, 1)
            self.setCentralWidget(splitter)
    
            highlight_dir = './floder1'
            self._it = QtCore.QDirIterator(highlight_dir)
    
            self._row, self._col = 0, 0
            QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
    
        def load_image(self):
            if self._it.hasNext():
                pixmap = QtGui.QPixmap(self._it.next())
                if pixmap.isNull():
                    return
                vlay = QtGui.QVBoxLayout()
                label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
                label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
                label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
                vlay.addWidget(label_pixmap)
                vlay.addWidget(label_text)
                vlay.addStretch()
                self._layout.addLayout(vlay, self._row, self._col)
                self._col += 1
                if self._col == 3:
                    self._row += 1
                    self._col = 0
    
        def on_clicked(self, text, pixmap):
            vlay = QtGui.QVBoxLayout()
            vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=text))
            vlay.addWidget(QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap))
            self._vbox.addLayout(vlay)
    
    if __name__ == '__main__':
        import sys
        app = QtGui.QApplication(sys.argv)
        w = MainWindow()
        w.showMaximized()
        sys.exit(app.exec_())
    

    Update:

    class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            scrollArea_left = QtGui.QScrollArea(widgetResizable=True)
            content_widget_left = QtGui.QWidget()
            scrollArea_left.setWidget(content_widget_left)
            self._layout = QtGui.QGridLayout(content_widget_left)
    
            self._label_pixmap = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
            self._label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter)
            widget = QtGui.QWidget()
            vbox = QtGui.QVBoxLayout(widget)
            vbox.addWidget(self._label_pixmap)
            vbox.addWidget(self._label_text)
    
            splitter = QtGui.QSplitter()
            splitter.addWidget(scrollArea_left)
            splitter.addWidget(widget)
            splitter.setStretchFactor(0, 3)
            splitter.setStretchFactor(1, 1)
            self.setCentralWidget(splitter)
    
            highlight_dir = './floder1'
            self._it = QtCore.QDirIterator(highlight_dir)
    
            self._row, self._col = 0, 0
            QtCore.QTimer(self, interval=1, timeout=self.load_image).start()
    
        def load_image(self):
            if self._it.hasNext():
                pixmap = QtGui.QPixmap(self._it.next())
                if pixmap.isNull():
                    return
                vlay = QtGui.QVBoxLayout()
                label_pixmap = ClickableLabel(alignment=QtCore.Qt.AlignCenter, pixmap=pixmap)
                label_text = QtGui.QLabel(alignment=QtCore.Qt.AlignCenter, text=self._it.fileName())
                label_pixmap.clicked.connect(partial(self.on_clicked, self._it.fileName(), pixmap))
                vlay.addWidget(label_pixmap)
                vlay.addWidget(label_text)
                vlay.addStretch()
                self._layout.addLayout(vlay, self._row, self._col)
                self._col += 1
                if self._col == 3:
                    self._row += 1
                    self._col = 0
    
        def on_clicked(self, text, pixmap):
            self._label_pixmap.setPixmap(pixmap)
            self._label_text.setText(text)