Menu

Using constructor with WrapperFactory

Sergey
2011-05-25
2013-04-06
  • Sergey

    Sergey - 2011-05-25

    Hello!
    I master PythonQt by means of examples, use Qt SDK 1.1 for Windows, PythonQt 2.0.1.
    I can not force to work Wrapper Factory in example PyCPPWrapperExample. At execution of a script examle.py the error stands out:

    py> 
    alternative 1 : CustomObject wrapped by decorators
    CustomObject (C++ Object 0x09B92F20)
    ['__dict__', '__doc__', '__init__', '__module__', '__weakref__', 'className', 'delete', 'firstName', 'help', 'lastName', 'setFirstName', 'setLastName']
    Mike Michels
    alternative 2 : CustomObject2 wrapped by factory
    CustomObject2 (C++ Object 0x09B92FA0)
    ['__dict__', '__doc__', '__init__', '__module__', '__weakref__', 'className', 'delete', 'help']
    Traceback (most recent call last):
      File ":example.py", line 35, in <module>
    AttributeError: CustomObject2 has no attribute named 'setFirstName'
    py>
    

    If the object is created not by the constructor, and with the slot of auxiliary object, for example, how is made in a test example PythonQtTestSlotCalling::testCppFactory():

      QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getHeight()==12: obj.setPassed();\n"));
      QVERIFY(_helper->runScript("if obj.createPQCppObject(12).getH()==12: obj.setPassed();\n"));
      QVERIFY(_helper->runScript("pq1 = obj.createPQCppObject(12);\n"
        "pq2 = obj.createPQCppObject(13);\n"
        "pq3 = obj.getPQCppObject(pq1);\n"
        "pq4 = obj.getPQCppObject(pq2);\n"
        "if pq3.getHeight()==12 and pq4.getHeight()==13: obj.setPassed();\n"
        ));
    

    it works. But all the same, the object created by the constructor, doesn't use WrapperFactory.
    What do I don't understand?

    Yours faithfully,
    Sergey.

     
  • Florian Link

    Florian Link - 2011-05-26

    Hi Sergey,

    the wrapper factory approach is quite old and nowadays we typically use decorators.
    I updated to examples shortly after the 2.0.1 release, maybe testing the SVN trunk version fixes the problem?

    On the otherhand, I am not sure if constructors ever worked together with wrapper factories, I would use decorator classes instead! Another workaround is to use a factory method instead of a constructor (just a slot that returns the new object, this can even be a static decorator, so that it is a factory method on the class)

    regards,
    Florian

    regards,
    Florian

     
  • Sergey

    Sergey - 2011-05-26

    Hello, Florian!
    Thanks for the help! I will try your councils, of course.

    I will explain, why I use the Factory approach. I need to wrap up mine C++ classes which have been not QObject derived. Thus I want to use Qt properties that in python scripts to write so: obj.attr = value. And even so: obj.subobj.attr = value.
    The slots of decorators need to pass a pointer to the wrapped object as the first argument. Hence, in the decorators I can not realize property Qt. Or I am mistaken?

    Yours faithfully,
    Sergey.

     
  • Florian Link

    Florian Link - 2011-05-26

    Hi Sergey,

    that is true. But you can emulate properties in the decorators like this:

    public slots:
      void py_set_yourProperty(YourClass* cpp, YourTypeOrQVariant value) { … }
      YourTypeOrQVariant py_get_yourProperty(YourClass* cpp) { … }

    This will add a python property named yourProperty to your wrapped C++ class.
    You can either use concrete types or QVariant for the value of the property.

    regards,
    Florian

     
  • Sergey

    Sergey - 2011-05-26

    Many thanks! I will try.

    Yours faithfully,
    Sergey.

     
  • Sergey

    Sergey - 2011-05-26

    It works! It is documentary feature?
    Yours faithfully,
    Sergey.

     
  • Florian Link

    Florian Link - 2011-05-26

    No, I actually forgot to document it, since I added it mainly for QStyleOption public member wrapping done by the code generator.

     
  • Sergey

    Sergey - 2011-05-26

    In my opinion, it is very useful feature. It saves the whole line of a code on each property! It is necessary to document it, that there was a confidence that in the future it will be supported.
    Thanks for library! Successes!
    Yours faithfully,
    Sergey.

     

Log in to post a comment.