Issue
I just want to extend a QPushButton so it would store an additional long string that I need afterwards.
Solution
The PyQt built-in classes support dynamic attributes, just like ordinary user-defined Python classes:
>>> button = QtWidgets.QPushButton()
>>> button.foo = 'bar'
>>> print(button.foo)
bar
However, you could also use QObject properties, since QPushButton
inherits QObject
(via QWidget
):
>>> button.setProperty('extra', 'additional long string')
>>> button.property('extra')
'additional long string'
One significant difference of this approach, is that unset properties will always return None
, rather than raising an error:
>>> button.property('whatever') is None
True
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.