I wish QtCreator had some helpful utilities like this one. It’s just boring to write generic getter/setter stuff for properties by hand.
cls = "BulletQMLBody"
names = ["velocityX", "velocityY", "velocityZ", "angleVelocityX", "angleVelocityY", "angleVelocityZ"]
names = ["float "+x for x in names]
HEADER_TEMPLATE = """
Q_PROPERTY(float %(NAME)s READ %(NAME)s WRITE %(SETTER)s)
%(TYPE)s %(NAME)s();
void %(SETTER)s(const %(TYPE)s %(NAME)s);
%(TYPE)s m_%(NAME)s;"""
IMPL_TEMPLATE = """
%(TYPE)s %(CLASS)s::%(NAME)s()
{
return m_%(NAME)s;
}
void %(CLASS)s::%(SETTER)s(const %(TYPE)s %(NAME)s)
{
m_%(NAME)s = %(NAME)s;
}"""
def getData(aCls,aType,aName):
return {
"CLASS": aCls,
"TYPE" : aType,
"NAME": aName,
"SETTER" : "set" + aName[0].upper() + aName[1:]
}
def genHeader(*args):
return HEADER_TEMPLATE % getData(*args)
def genImpl(*args):
return IMPL_TEMPLATE % getData(*args)
for n in names:
t,n = n.split();
print genHeader(cls, t, n)
for n in names:
t,n = n.split()
print genImpl(cls, t, n)
Output:
Q_PROPERTY(float velocityX READ velocityX WRITE setVelocityX)
float velocityX();
void setVelocityX(const float velocityX);
float m_velocityX;
...
float BulletQMLBody::velocityX()
{
return m_velocityX;
}
void BulletQMLBody::setVelocityX(const float velocityX)
{
m_velocityX = velocityX;
}
...