[PySide] Questions about QProperties

Christian Gagneraud chris at techworks.ie
Thu Jul 26 12:26:01 CEST 2012


On 25/07/12 17:52, Christian Gagneraud wrote:
> Hi list,
>
> [I'm using PySide-1.1.0 and Qt-4.7.4 on an OpenSuse-12.1]
>
> I don't if it's me doing something wrong, but I'm having trouble getting
> basic things working with QProperties and inheritance.
>
> First I tried to define properties using the decorator syntax like this:
>
> @QtCore.Property(float)
> def toto(self)
>     return self._toto
>
> @toto.setter(self, value)
>     self._toto = value
>
> And it simply doesn't work, the setter is never called.
>
> Then i decided to go with:
> toto = QtCore.Property(float, get_toto, set_toto)
>
> And it worked as expected, until I start using class derivation. Here is
> a basic use case:
>
> from PySide import QtCore
>
> class Obj1(QtCore.QObject):
>
>       def __init__(self):
>           super(Obj1, self).__init__()
>           self._toto = 0.0
>
>       def get_toto(self):
>           print "get toto"
>           return self._toto
>
>       def set_toto(self, value):
>           print "set toto"
>           self._toto = value
>
>       toto = QtCore.Property(float, get_toto, set_toto)
>
> class Obj2(QtCore.QObject):
              ^^^^^^^^^^^^^^
Stupid me! Should be Obj2(Obj1). Now it works as expected.

>
>       def __init__(self):
>           super(Obj2, self).__init__()
>
> o1 = Obj1()
> print o1.property("toto")
> o1.setProperty("toto", 1.23)
> print o1.property("toto")
> print ""
> o2 = Obj2()
> print o2.property("toto")
> o2.setProperty("toto", 1.23)
> print o2.property("toto")
>
> And here is the output:
> get toto
> 0.0
> set toto
> get toto
> 1.23
>
> None
> 1.23
>
> So what happens here is obj1.toto works as expected, but obj2.toto
> doesn't, a new property "toto" is being created on obj2 and
> Obj1::get_toto is never called.
> I would expect to be able to set an Obj1 property on an Obj2 instance
> when I call obj2.setProperty(...)
> At least, this work in C++, but it doesn't work in my case.
>
> This sounds so basic, that I couldn't believe it! So either i'm doing
> something wrong, or the python behaviour of a QObject is different from
> the C++ version, which sounds really odd to me....
>
> Chris
>
>
>


-- 
Christian Gagneraud,
Embedded systems engineer.
Techworks Marine
1 Harbour road
Dun Laoghaire
Co. Dublin
Ireland
Tel: + 353 (0) 1 236 5990
Web: http://www.techworks.ie/





More information about the PySide mailing list