#ifndef_PYCUSTOMOBJECTS_H#define_PYCUSTOMOBJECTS_H#include"PythonQt.h"#include"PythonQtCppWrapperFactory.h"#include"PythonQtObjectPtr.h"#include<QObject>namespacetest{classCustomObject2{public:CustomObject2() {_firstName2="Richard";_lastName2="Black";}CustomObject2(constQString&first2,constQString&last2){_firstName2=first2;_lastName2=last2;}QString_firstName2;QString_lastName2;};// declare our own custom objectclassCustomObject{public:CustomObject() {//m_customshared = QSharedPointer<CustomObject2>(new CustomObject2);}CustomObject(constQString&first,constQString&last){_firstName=first;_lastName=last;}QString_firstName;QString_lastName;//typedef QSharedPointer<CustomObject2> m_customshared;QSharedPointer<CustomObject2>m_customshared=QSharedPointer<CustomObject2>(newCustomObject2);//typedef QSharedPointer<CustomObject2> m_customshared;};// add a decorator that allows to access the CustomObject from PythonQtclassCustomObjectWrapper : publicQObject{Q_OBJECTpublicQ_SLOTS:// add a constructorCustomObject*new_CustomObject(constQString&first,constQString&last){returnnewCustomObject(first,last);}// add a destructorvoiddelete_CustomObject(CustomObject*o){deleteo;}// add access methodsQStringfirstName(CustomObject*o){returno->_firstName;}QStringlastName(CustomObject*o){returno->_lastName;}voidsetFirstName(CustomObject*o,constQString&name){o->_firstName=name;std::cout<<"bazdmeg\n";std::cout<<o->m_customshared->_firstName2.toStdString().c_str()<<"\n";}voidsetLastName(CustomObject*o,constQString&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)publicQ_SLOTS:
QSharedPointer<CustomObject2>customObject2(CustomObject*o){returno->m_customshared;}QSharedPointer<CustomObject2>customObject2(CustomObject*o)const{returno->m_customshared;}voidsetCustomObject2(CustomObject*o,QSharedPointer<CustomObject2>customObject2){o->m_customshared=customObject2;}voidprint_cus_fn(CustomObject*o,QStringlastn){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{returno->m_customshared;}/* QSharedPointer<CustomObject2> folder() { return m_customshared; } void setFolder(QSharedPointer<CustomObject2> folder) { m_customshared = folder; }*/};classCustomObject2Wrapper : publicQObject{Q_OBJECTpublicQ_SLOTS:// add a constructorCustomObject2*new_CustomObject2(constQString&first2,constQString&last2){returnnewCustomObject2(first2,last2);}// add a destructorvoiddelete_CustomObject2(CustomObject2*o){deleteo;}// add access methodsQStringfirstName2(CustomObject2*o){returno->_firstName2;}QStringlastName2(CustomObject2*o){returno->_lastName2;}voidsetFirstName2(CustomObject2*o,constQString&name2){o->_firstName2=name2;}voidsetLastName2(CustomObject2*o,constQString&name2){o->_lastName2=name2;}};}main.cpplikethis:/* * * 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>intmain(intargc,char**argv){QApplicationqapp(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);PythonQtObjectPtrmainContext=PythonQt::self()->getMainModule();PythonQtScriptingConsoleconsole(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();returnqapp.exec();}example.pyis:
fromPythonQt.exampleimport*importinspectimportosprint(os.getcwd())print("CustomObject wrapped by decorators")#createanewobjectcustom=CustomObject("John","Doe")#uj=Uj("John","Doe")#printtheobject(toseehowitiswrapped)print(custom)#print(uj)#printthemethodsavailableprint(dir(custom))#print(dir(uj))#setanamecustom.setFirstName("Mike")custom.setLastName("Michels")#custom.setnevfirst("valami")#uj.setFirstName("szeva")#uj.setLastName("mester")#custom.setccustomObject(custom)#getthenameprint(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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is the pycustomobject.h 's code:
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?
Returning QSharedPointer instances to Python is not supported by PythonQt.
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?
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.