Remove an Image in QLabel After Button Clicked

Cahit Yıldırım picture Cahit Yıldırım · Dec 30, 2015 · Viewed 11.9k times · Source

I have qlabels that displaying images . I want to delete image if user clicks remove button . I can learn which image clicked

labels[i].mousePressEvent = functools.partial(self.remove_image, source_label = labels[i] ,source_image = pixmap)

but i couldn't use it and connects with button . How can i remove image ?

Answer

Iron Fist picture Iron Fist · Dec 30, 2015

Assuming labels[] has a list of labels ID, I think you can do something like:

labels[i].mousePressEvent = functools.partial(self.remove_image, source_label = labels[i]) #just pass to self.remove_image the label id

Then in self.remove_image and since label.clear() (to clear content of label) is a SLOT then, you can connect it to clicked signal directly:

def remove_image(self, label_id):
    QtCore.QObject.connect(self.deleteButton, QtCore.SIGNAL("clicked()"), label_id.clear)