[PyQt] Getting image filename in QLabel->pixmap

David Boddie david at boddie.org.uk
Sat Jun 4 15:11:41 BST 2011


On Fri, 3 Jun 2011 18:12:49 -0700 (PDT), James Polk wrote:

> Let's say you create a QLabel and make it an icon, by associating an image
> file to it,.. like..
>
> ????? thisPixmap? = QtGui.QPixmap('server:/images/airplane.jpg') )
>
> ????? thisIcon = QtGui.QLabel(self)
> ????? thisIcon.setPixmap(thisPixmap)
>
> Now let's say that later, in another function...you need to query
> "thisIcon" to obtain the name of the image file associated with it
> ("airplane.png"). How can one do this?? I can't find any function in either
> the QLabel nor the QPixmap class that has a way to return the underlying
> image filename.

QPixmap just represents the image data. It only records the data, not the
name of the file it came from. It's a similar situation with QIcon. You
need to store the name somewhere yourself.

If you keep a reference to the label, one way to record the file name might
be to store it as an attribute of the label.

  thisPixmap = QtGui.QPixmap('server:/images/airplane.jpg')
  self.thisIcon = QtGui.QLabel(self)
  self.thisIcon.setPixmap(thisPixmap)
  self.thisIcon.fileName = 'server:/images/airplane.jpg'

Or you could keep a dictionary that maps labels to file names. It depends
on how you are organising your application.

David


More information about the PyQt mailing list