Remove an Image in QLabel After Button Clicked

15,091

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)
Share:
15,091
Cahit Yıldırım
Author by

Cahit Yıldırım

Updated on June 04, 2022

Comments

  • Cahit Yıldırım
    Cahit Yıldırım about 2 years

    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 ?

    • Iron Fist
      Iron Fist over 8 years
      Are these QLabels in a listWidget?
    • Cahit Yıldırım
      Cahit Yıldırım over 8 years
      @IronFist No , QLabels in Frame . Does it matter ?
    • Iron Fist
      Iron Fist over 8 years
      Okey...I was just thinking if these QLabels where in a listWidget it would be easy to delete them by selecting the ones and deleting them at once. But in your case, you want to delete the image clicked by user, is it going to be deleting one image per click or multiple images at one click?
    • Cahit Yıldırım
      Cahit Yıldırım over 8 years
      Deleting One image per click . Above code examples i can get which QLabel clicked but i can't make connect with button click .