我想在垂直layout.i中显示我的可点击图片。我从Google得到了类似的示例,但我不知道如何从可点击标签中获取图片。所以如果我选择了其他项目,谁能帮助我在vbox中添加图片,那么第一个项目将在垂直框中删除。我只想显示最后一个可点击的项目。在这里,我尝试了很多方法,但我得到了object。事先谢谢您。
下面是示例:

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)

关于python - 如何在Verticalbox布局中获取选定的最后可点击图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53995267/

10-13 05:47