[PyQt] List of instances of a class

Florian Bruhin me at the-compiler.org
Tue Oct 28 05:57:19 GMT 2014


Hi,

* Chris O'Halloran <cmoman at gmail.com> [2014-10-28 11:05:45 +1300]:
> This widget has a number of QtGui objects such as QDial, QSpinbox, QLabel
> and links them with some signals and slots.
> 
> Later on in my code I instantiate a number of these classes and lay them
> out on a MainWindow widget.
> 
> [...]

> I've found myself repeating a bit of code so I wondered if could gather
> these objects into a list.
> 
> I find I can using
> 
> self.dialist = self.findChildren(InputDial)
> 
> [<__main__.InputDial object at 0x0000000003BF6EE8>, <__main__.InputDial
> object at 0x0000000005ABB1F8>,  etc
> 
> This return a list of objects and I can access them
> 
> self.dialist[0] etc
> 
> At this stage though, I'd like to be able to return the 'handle' (I'm new
> to a lot of this') of when I originally instantiated the class eg self.Rf.
> self.diallist[0].objectname() only return what you assign it and not the
> handle.

I don't follow. Looking at the output above, you get a reference to
the instance of your class, i.e. to the same thing self.Rf etc. would
be.

> I've been searching through Object.metaclass() but am not
> convinced I'm looking in the right place. self.dialist[0].__getAttr__()
> seems to want a parameter but I'm not sure what to give it.

You probably don't want to deal with Qt metaclasses, and you don't
want to call __getattr__ by hand. If anything, you'd use
getattr(self, name).

But either way, that sounds like an overcomplicated way. I suggest
either keeping a dict of all objects:

    def __init__(self, parent=None):
        super().__init__(parent)
        self.dials = {
            'Rf': InputDial('Fault Resistance', 'Ohms', 0, 400),
            # [...]
        }

    def foo(self):
        for dial in self.dials.values():
            frobulize(dial)

or keeping the attributes and storing a list:

    def __init__(self, parent=None):
        super().__init__(parent)
        self.Rf = InputDial('Fault Resistance', 'Ohms', 0, 400)
        # [...]
        self.dials = [self.Rf, ...]

    def foo(self):
        for dial in self.dials:
            frobulize(dial)

But let's take another step back and look at the big picture. Why do
you want a list of all dials? What are you going to do with them?
What is the repetitive code you want to get rid of?

Maybe what you really should do is using Qt's signal/slot system, add
some signal, and connect all dial's slots to it, and then forget about
your dials?

> Would this question be better suited for stackoverflow?

I think both StackOverflow and this ML are good ressources. If you're
not satisfied with one, try another. I'm not following SO, but I've
gotten many good answers there in the past.

Florian

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
             GPG 0xFD55A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141028/d9c3d543/attachment.sig>


More information about the PyQt mailing list