Если вы хотите, чтобы QLabel уведомлял вас о щелчке, проще всего написать его, как указано в этом ответе .
С другой стороны, лучшечто вы используете 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)