Menu

Pass a sharedpointer

Help
Ferenc
2016-06-01
2016-06-02
  • Ferenc

    Ferenc - 2016-06-01

    Here is the pycustomobject.h 's code:

    #ifndef _PYCUSTOMOBJECTS_H
    #define _PYCUSTOMOBJECTS_H
    
    #include "PythonQt.h"
    #include "PythonQtCppWrapperFactory.h"
    #include "PythonQtObjectPtr.h"
    
    #include <QObject>
    
    namespace test{
    class CustomObject2 {
    public:
      CustomObject2() {_firstName2 = "Richard"; _lastName2 = "Black";}
      CustomObject2(const QString& first2, const QString& last2) { _firstName2 = first2; _lastName2 = last2; }
      QString _firstName2;
      QString _lastName2;
    
    };
    
    // declare our own custom object
    class CustomObject {
    public:
      CustomObject() {
          //m_customshared = QSharedPointer<CustomObject2>(new CustomObject2);
                     }
      CustomObject(const QString& first, const QString& last) { _firstName = first; _lastName = last; }
    
      QString _firstName;
      QString _lastName;
      //typedef QSharedPointer<CustomObject2> m_customshared;
      QSharedPointer<CustomObject2> m_customshared =
             QSharedPointer<CustomObject2>(new CustomObject2);
      //typedef QSharedPointer<CustomObject2> m_customshared;
    
    };
    
    // add a decorator that allows to access the CustomObject from PythonQt
    class CustomObjectWrapper : public QObject {
    
      Q_OBJECT
    
    public Q_SLOTS:
      // add a constructor
      CustomObject* new_CustomObject(const QString& first, const QString& last) { return new CustomObject(first, last); }
    
      // add a destructor
      void delete_CustomObject(CustomObject* o) { delete o; }
    
      // add access methods
    
      QString firstName(CustomObject* o) { return o->_firstName; }
    
      QString lastName(CustomObject* o) { return o->_lastName; }
    
      void setFirstName(CustomObject* o, const QString& name) { o->_firstName = name; std::cout<<"bazdmeg\n";
                                                                std::cout<<o->m_customshared->_firstName2.toStdString().c_str()<<"\n"; }
    
      void setLastName(CustomObject* o, const QString& name) { o->_lastName = name; }
    
    public:
    
      //Q_PROPERTY(QSharedPointer<CustomObject> CcustomObject READ ccustomObject WRITE setccustomObject)
    
     // Q_PROPERTY(QSharedPointer<CustomObject> CustomObject2 READ customObject2 WRITE setCustomObject2)
     // typedef QSharedPointer<QMap<CustomObject, QSharedPointer<CustomObject>>> t_type;
    
      //Q_PROPERTY(t_type CustomObject2 READ customObject2 WRITE setCustomObject2)
    
    public Q_SLOTS:
       QSharedPointer<CustomObject2> customObject2(CustomObject* o)
       {
           return o->m_customshared;
       }
       QSharedPointer<CustomObject2> customObject2 (CustomObject* o) const
       {
           return o->m_customshared;
       }
    
       void setCustomObject2(CustomObject* o, QSharedPointer<CustomObject2> customObject2)
       {
           o->m_customshared = customObject2;
    
       }
       void print_cus_fn(CustomObject* o, QString lastn){
           o->m_customshared->_firstName2 = "Feri";
           o->m_customshared->_lastName2 = lastn;
    
           qDebug()<<o->m_customshared->_firstName2.toStdString().c_str();
           qDebug()<<o->m_customshared->_lastName2.toStdString().c_str();
    
       }
       //Q_PROPERTY(QSharedPointer<CustomObject> CcustomObject READ ccustomObject WRITE setccustomObject)
      // Q_PROPERTY(QSharedPointer<CustomObject2> ccustomObject READ ccustomObject WRITE setccustomObject)
    
       //QSharedPointer<CustomObject2> m_customshared;
    
       QSharedPointer<CustomObject2> customshared(CustomObject* o) const
       {
           return o->m_customshared;
       }
    /*
       QSharedPointer<CustomObject2> folder()
       {
           return m_customshared;
       }
    
       void setFolder(QSharedPointer<CustomObject2> folder)
       {
           m_customshared = folder;
       }*/
    
    };
    class CustomObject2Wrapper : public QObject {
    
      Q_OBJECT
    
    public Q_SLOTS:
      // add a constructor
      CustomObject2* new_CustomObject2(const QString& first2, const QString& last2) { return new CustomObject2(first2, last2); }
    
      // add a destructor
      void delete_CustomObject2(CustomObject2* o) { delete o; }
    
      // add access methods
    
      QString firstName2(CustomObject2* o) { return o->_firstName2; }
    
      QString lastName2(CustomObject2* o) { return o->_lastName2; }
    
      void setFirstName2(CustomObject2* o, const QString& name2) { o->_firstName2 = name2; }
    
      void setLastName2(CustomObject2* o, const QString& name2) { o->_lastName2 = name2; }
    };
    
    }
    
    main.cpp like this:
    
    /*
     *
     *  Copyright (C) 2010 MeVis Medical Solutions AG All Rights Reserved.
     *
     *  This library is free software; you can redistribute it and/or
     *  modify it under the terms of the GNU Lesser General Public
     *  License as published by the Free Software Foundation; either
     *  version 2.1 of the License, or (at your option) any later version.
     *
     *  This library is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     *  Lesser General Public License for more details.
     *
     *  Further, this software is distributed without any warranty that it is
     *  free of the rightful claim of any third person regarding infringement
     *  or the like.  Any license provided herein, whether implied or
     *  otherwise, applies only to this software file.  Patent licenses, if
     *  any, provided herein do not apply to combinations of this program with
     *  other software, or any other product whatsoever.
     *
     *  You should have received a copy of the GNU Lesser General Public
     *  License along with this library; if not, write to the Free Software
     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     *
     *  Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
     *  28359 Bremen, Germany or:
     *
     *  http://www.mevis.de
     *
     */
    
    //----------------------------------------------------------------------------------
    /*!
    // \file    PyGuiExample.cpp
    // \author  Florian Link
    // \author  Last changed by $Author: florian $
    // \date    2007-04
    */
    //----------------------------------------------------------------------------------
    
    #include "PythonQt.h"
    #include "gui/PythonQtScriptingConsole.h"
    
    #include "CustomObjects.h"
    
    #include <QApplication>
    
    int main( int argc, char **argv )
    {
      QApplication qapp(argc, argv);
      /*int id = QMetaType::type("m_customshared");
      qDebug()<<id;
      if (id != QMetaType::UnknownType) {
          void *m_customsharedPtr = QMetaType::create(id);
          ...
          QMetaType::destroy(id, m_customsharedPtr);
          m_customsharedPtr = 0;
          qDebug()<<id;
    
      }*/
    
      PythonQt::init(PythonQt::RedirectStdOut | PythonQt::ExternalHelp);
      PythonQtObjectPtr  mainContext = PythonQt::self()->getMainModule();
    
      PythonQtScriptingConsole console(NULL, mainContext);
    
      // register the new object as a known classname and add it's wrapper object
      //qRegisterMetaType<CustomObject::m_customshared<CustomObject2>>("CustomObject");
      PythonQt::self()->registerCPPClass("CustomObject", "","example", PythonQtCreateObject<test::CustomObjectWrapper>);
      PythonQt::self()->registerCPPClass("CustomObject2", "","example", PythonQtCreateObject<test::CustomObject2Wrapper>);
    
      mainContext.evalFile(":femkeres_szamla.py");
      std::cout<<"femkeres_szamla lefutott\n";
      mainContext.evalFile(":femkeres_irasbeli.py");
      std::cout<<"femkeres_irasbeli lefutott\n";
    
      mainContext.evalFile(":femkeres_jegyzokonyv.py");
      std::cout<<"femkeres_jegyzokonyv lefutott\n";
    
      mainContext.evalFile(":femkeres_anyagkisero.py");
      std::cout<<"femkeres_anyagkisero lefutott\n";
    
      mainContext.evalFile(":example.py");
      std::cout<<"example lefutott\n";
    
      console.show();
    
      return qapp.exec();
    }
    
    example.py is:
    
    from PythonQt.example import *
    import inspect
    import os
    
    print (os.getcwd())
    
    print("CustomObject wrapped by decorators")
    
    # create a new object
    custom = CustomObject("John","Doe")
    #uj = Uj("John","Doe")
    
    # print the object (to see how it is wrapped)
    print (custom)
    #print (uj)
    
    # print the methods available
    print (dir(custom))
    #print (dir(uj))
    
    # set a name
    custom.setFirstName("Mike")
    custom.setLastName("Michels")
    #custom.setnevfirst("valami")
    #uj.setFirstName("szeva")
    #uj.setLastName("mester")
    #custom.setccustomObject(custom)
    
    # get the name
    print (custom.firstName() + " " + custom.lastName())
    #print (custom.nevfirst() + " " + custom.lastName())
    print (custom.print_cus_fn("White"))
    print (custom.customshared()._firstName2)
    

    My problem is: when i try to run the program, then produces a the following output:

    Mike Michels
    None
    Traceback (most recent call last):
    File ":example.py", line 50, in <module>
    ValueError: Called customshared() -> QSharedPointer<CustomObject2>, return type 'QSharedPointer<CustomObject2>' is ignored because it is unknown to PythonQt. Probably you should register it using qRegisterMetaType() or add a default constructor decorator to the class.

    How can i access to the qsharedpointer's class methods, like _firstname2?

     
  • Florian Link

    Florian Link - 2016-06-01

    Returning QSharedPointer instances to Python is not supported by PythonQt.

     
  • Ferenc

    Ferenc - 2016-06-02

    Thank you for the quick response. We are using a lot of QSharedPointer-s in our program, then we need to find an other solution. Will you plan to support these smart pointers in PythonQt in the future?

     
  • Florian Link

    Florian Link - 2016-06-02

    No, I don't have plans to add support for QSharedPointer. I think you should create a new scripting Api without shared pointers that wraps your internal Apis.

    Adding support for QSharedPointer would require quite some extensions to the PythonQt wrappers and template magic, since PythonQt would need to create a QSharedPointer of the correct type and keep that around as long as the Python wrapper lives.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.