Add custom attribute to QCheckBox widget
Asked Answered
L

2

5

I have (I think) a simple question but haven't had much luck trying to find an answer. Really new to pyqt!

I am dynamically adding a number of QtGui.QCheckBox() widgets to a gridLayout based on a number of factors. My question is, how can I add a custom attr to each chkbox widget? I want to store a few custom things inside each qt widget.

Thanks for any help. A basic example would be most useful.

Cheers

Larry answered 4/6, 2015 at 19:46 Comment(0)
S
2

You can just subclass the QCheckBox class. For example:

class MyCheckBox(QtGui.QCheckBox):
    def __init__(self, my_param, *args, **kwargs):
        QtGui.QCheckBox.__init__(self, *args, **kwargs)
        self.custom_param = my_param

Here we override the __init__ method which is called automatically when you instantiate the class. We add an extra parameter my_param to the signature and then collect any arguments and keyword arguments specified into args and kwargs.

In our new __init__ method, we first call the original QCheckBox.__init__ passing a reference to the new object self and unpacking the arguments are keyword arguments we captured. We then save the new parameter passed in an an instance attribute.

Now that you have this new class, if you previously created (instantiated) checkbox's by calling x = QtGui.QCheckBox('text, parent) you would now call x = MyCheckBox(my_param, 'text', parent) and you could access your parameter via x.custom_param.

Sanyu answered 5/6, 2015 at 0:10 Comment(0)
D
9

You can also use the .setProperty() method, aka Dynamic Properties:

self.the_wdiget.setProperty("my_string", "hello")
self.the_wdiget.setProperty("my_bool", True)
self.the_wdiget.setProperty("my_int", 10)
self.the_wdiget.setProperty("my_stringList", ['sl1', 'sl2', 'sl3'])

# And get it by:

self.the_widget.property("my_bool") # etc.

Strings can also be set to translateable. E.g.

self.the_widget.setProperty("my_string", _translate("Dialog", "hello"))

http://doc.qt.io/qt-5/qobject.html#setProperty

Also see:

http://pyqt.sourceforge.net/Docs/PyQt5/qt_properties.html

Disingenuous answered 20/3, 2018 at 0:5 Comment(0)
S
2

You can just subclass the QCheckBox class. For example:

class MyCheckBox(QtGui.QCheckBox):
    def __init__(self, my_param, *args, **kwargs):
        QtGui.QCheckBox.__init__(self, *args, **kwargs)
        self.custom_param = my_param

Here we override the __init__ method which is called automatically when you instantiate the class. We add an extra parameter my_param to the signature and then collect any arguments and keyword arguments specified into args and kwargs.

In our new __init__ method, we first call the original QCheckBox.__init__ passing a reference to the new object self and unpacking the arguments are keyword arguments we captured. We then save the new parameter passed in an an instance attribute.

Now that you have this new class, if you previously created (instantiated) checkbox's by calling x = QtGui.QCheckBox('text, parent) you would now call x = MyCheckBox(my_param, 'text', parent) and you could access your parameter via x.custom_param.

Sanyu answered 5/6, 2015 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.