Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[shiboken2] Shared pointer support



  • I want to wrap a library which is based on QT and which uses shared pointers. I stumbled over a few issues, one of them seems to be a showstopper:

    • It seems that wrapping classes which return shared pointers in a virtual method is not possible at all. Non-virtual functions are fine. The error from shiboken2 is "#error: CppGenerator::writeVirtualMethodNative: B::genA(): Could not find a minimal constructor for type 'QSharedPointer<A >'. This will result in a compilation error.".

    • I'm not sure about what can be expected from the shared pointer bridging. While I'm able to pass shared pointers which are generated from the c++ side around in python, it seems to be impossible to create a shared pointer from an object generated from the python side.

    • The documentation in shiboken regarding smart pointers seems to be very limited. I couldn't figure out if setting "ref-count-method" is required and why? For QSharedPointer, there doesn't seem to exist a method for querying the reference count.

    The first point looks like a shiboken2 bug to me and I'm wondering if there are any workarounds here?

    Here are my test files:

    test.h

    #ifndef TEST_H
    #define TEST_H
    
    #include <QtCore/QSharedPointer>
    
    // if uncommented, you will get the following error when calling shiboken
    // #error:  CppGenerator::writeVirtualMethodNative: B::genA(): Could not find a minimal constructor for type 'QSharedPointer<A >'. This will result in a compilation error.
    #define SHIBOKEN_ERROR
    
    class A
    {
        public:
            int a;
            A(int _a);
            virtual ~A();
    };
    
    class B
    {
        public:
            int b;
            B(int _b);
            virtual ~B();
            void doSomething(QSharedPointer<A> a);
            #ifdef SHIBOKEN_ERROR
            virtual
            #endif
            QSharedPointer<A> genA();
    };
    
    #endif
    
    

    ptest.xml

    <!--?xml version="1.0"?-->
    <typesystem package="ptest">
        <load-typesystem name="typesystem_core.xml" generate="no" />
    
        <smart-pointer-type name="QSharedPointer" type="shared" getter="data" />
    
        <object-type name="A" />
        <object-type name="B" />
    </typesystem>
    

    ptest .py

    import PySide2
    import ptest
    
    a=ptest.A(1)
    b=ptest.B(2)
    
    a2=b.genA()
    print(a2.a)
    b.doSomething(a2)
    
    # following is not working, need to create a shared pointer from the python object a
    b.doSomething(a)
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since it's on the shiboken level, I would recommend bringing your question to the PySide mailing list. You'll find there PySide2 developers/maintainers. This forum is more user oriented.



  • Thanks for answering - I did so :)


Log in to reply