Reputation: 529
I have a lot of qlabels
that display images. When click this qlabels mousePressEvent
returns which object clicked and I show this images in another qlabel
with big size.Then I can delete image using qpushbutton
.If users click more qlabels
and then click qpushbutton
, all of images deleted. I want users delete an image per click.So I write a method that delete images but I can't control if user clicks a lot of images and deleting them . How can I do that ?
labels[i].mousePressEvent = functools.partial(self.label_click, source_label = labels[i] ,source_image = pixmap)
def label_click(self, event,source_label=None, source_image=None):
self.labelDisplayBigImage.setPixmap(source_image)
self.labelDisplayBigImage.setScaledContents(True)
if(source_label.picture() == self.labelDisplayBigImage.picture()):
self.btnCancel.clicked.connect(source_label.clear)
self.btnCancel.clicked.connect(self.labelDisplayBigImage.clear)
Upvotes: 0
Views: 448
Reputation: 4306
you can use QListWidget()
. The default selectionMode()
of QListWidget
is singleSelection.
Either add the images directly as QIcon
to the listwidget and set an appropriate iconSize:
class MyList(QtWidgets.QListWidget):
def __init__(self):
QtWidgets.QListWidget.__init__(self)
images = ['IMG_1.jpeg', 'IMG_2.jpeg', 'IMG_3.jpeg', 'IMG_4.jpeg', 'IMG_5.jpeg']
for i in images:
icon = QtGui.QIcon(i)
item = QtWidgets.QListWidgetItem(icon, i)
self.addItem(item)
self.setIconSize(QtCore.QSize(150,100))
self.setSelectionMode(1) # 1 = SingleSelection, 2 = MultiSelection, not necessary, default mode is singleSelection
self.setGeometry(200,200,300,500)
or add labels containing the images to the listwidget, then an appropriate sizeHint for the item must be given:
for i in images:
label = QtWidgets.QLabel(self)
pm = QtGui.QPixmap(i)
label.setPixmap(pm.scaled(QtCore.QSize(150,100)))
item = QtWidgets.QListWidgetItem(i)
item.setSizeHint(QtCore.QSize(200,110))
self.addItem(item)
self.setItemWidget(item,label)
connect to currentItemChanged-signal to get the selected item, (cave: this signal sends 2 items, the current and the previous)
self.currentItemChanged.connect(self.findSel)
def findSel(self, current, previous):
print(current.text())
try:
print(previous.text())
except AttributeError:
print('first selection, no previous item!')
Upvotes: 1