how to get the picture's full path name on the button?

797 views
Skip to first unread message

张宇

unread,
May 19, 2012, 5:10:45 AM5/19/12
to python_in...@googlegroups.com
With this code i add an image to a button, but i don't know how to get the picture's name with code
self.ptBn.setIcon(QtGui.QIcon(os.path.join(iconPath, 'pin.png')))
And i add a new custom property with QtDesigner, i don't know how to return the value of this property
self.pushButton.setProperty(_fromUtf8("state"), False)

--
My Blog     Maya Programing BBS


Justin Israel

unread,
May 19, 2012, 11:50:57 AM5/19/12
to python_in...@googlegroups.com
For the QIcon question, you can't query the filename that was used to set the image on the object, because technically it could have been set by passing in a QPixmap that was already up.
What you can do is just set an attribute on the QIcon and store the filename manually:

path = os.path.join(iconPath, 'pin.png')
icon = QtGui.QIcon(path)
icon.path = path
self.ptBn.setIcon(icon)
print icon.path

Normally you don't need to know the path as a bit of data from the QIcon. But maybe you really need to subclass it and add some more custom functionality to suit your goal.

For the custom property on the QPushButton, its not normally common to use the dynamic properties of QtDesigner when designing for PyQt4. Its more of a benefit to C++. The reason is because in python you can freely add attributes to the objects. Using the dynamic property in pyqt is even more obscure because it makes you go through a QVariant return type.
But, if you set a bool dynamic property on your QPushButton in designer, you would use it like this:

        print self.pushButton.property("state").toBool()
        self.pushButton.setProperty("state", False)
        print self.pushButton.property("state").toBool()

But like I said, this is a lot more obscure than if you had just used an attribute on the fly like: self.pushButton.foo = False
Or again, subclass your own QPushButton to add custom functionality. 

Most PyQt4 developers I think mainly use QtDeveloper for the visual layout to create a view, and leave all extended logic for code.


Reply all
Reply to author
Forward
0 new messages