Menu

Support for QMap<int, MyObjectPtr>

Anonymous
2013-01-04
2013-04-06
  • Anonymous

    Anonymous - 2013-01-04

    Hi you all. I have the following problem that I would like to solve. I want to expose an internal QMap structure externally to python as dictionary.

    The code is like that:

    Q_DECLARE_METATYPE(MyObject*)

    class MyClass : public QObject

        Q_OBJECT
        public slots:
            QList<MyObject*> getlist();

            QMap<int,MyObject*> getIntegerKeyMap()
            {
                QMap<int,MyObject*> tmp;
                tmp.insert(1, new MyObject());
                return  tmp;
            }
            QVariantMap getVariantKeyMap()
            {
                 QVariantMap tmp;
                 MyObject *myobjptr = new MyObject();
                tmp.insert(QString::number(1),QVariant::fromValue(myobjptr));
                return  tmp;
            }
    }

    I have registered the class MyObject and I've already made the same thing exposing a QList<MyObjectPtr> to Python which works fine, because the conversion is done by PythonQtConversion.cpp

    When I try to define a method with return value of type QMap<int, MyObjectPtr> I can compile it but at runtime when I call that method I get the following error:

    ValueError: Called QMap<int,MyObjectPtr*> aaa(), return type 'QMap<int,MyObjectPtr*>' is ignored because it is unknown to PythonQt. Probably you should register it using qRegisterMetaType() or add a default constructor decorator to the class.

    I don't want to use the QVariantMap version because I want the keys to be integers, not strings. How can I solve this problem?
    Should I write a new custom conversion inside PythonQtConversion?

     
  • Florian Link

    Florian Link - 2013-01-05

    You do this externally by using

    PythonQtConv::registerMetaTypeToPythonConverter

    to register a conversion method. You will need the meta type id of your map, which is returned from qRegisterMetaType().
    See the calls to registerMetaTypeToPythonConvert in PythonQt to see how it is done. You will need to
    do the conversions manually, wrapping your objects to Python can be done with PythonQt::priv()->wrapPtr().

    Hope this helps.

     

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.