如何在垂直框布局中获取选定的最后可点击图像

how to get the selected last clickable image in verticalbox layout

我想在垂直方向显示我的可点击图像 layout.i 从 google 得到了类似的例子,但我不知道如何从可点击标签中获取图像。如果我选择了其他项目,那么任何人都可以帮我在 vbox 中添加图像,然后第一个项目将在垂直方向上删除 box.i 只想显示最后一个可点击的项目。在这里我尝试了很多方法,但我提前得到了 object.Thank 你。 以下是示例:

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

如果你想让一个QLabel在有点击的时候通知你,最简单的就是按照this answer中的说明来写。

另一方面,最好使用 QScrollArea 作为 QVBoxLayout 的基础,因为数量太多将无法将它们全部可视化。

最后我更喜欢使用 QSplitter,因为它可以轻松更改小部件的大小。

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

更新:

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)