Info: Qt 4.8, QtQuick 1.1
Forgive me if my google foo is too weak...


I would like to expose a dataset that is based on QSharedPointer and QWeakPointer to a QML component. The basis of the dataset is as simple as:

Qt Code:
  1. class LogicRelation : public QObject
  2. {
  3. Q_OBJECT
  4. public slots:
  5. ....
  6. QSharedPointer<LogicModel> fromObject ();
  7. QSharedPointer<LogicModel> toObject ();
  8. ....
  9. };
  10. class LogicModel : public QObject
  11. {
  12. Q_OBJECT
  13. ...
  14. };
  15.  
  16.  
  17. Q_DECLARE_METATYPE( QSharedPointer<LogicModel> )
  18. Q_DECLARE_METATYPE( QSharedPointer<LogicRelation> )
To copy to clipboard, switch view to plain text mode 

I will confess that just as much as I like QML for its ability to bind properties, I equally find myself resorting to guesswork in ten out of ten cases when it comes to grokking the type system, how to register types and how to declare them as Component properties.
I can't register a QSharedPointer in QML (obviously, since QSharedPointer doesn't inherit from QObject).

Imagine a QML file:
Qt Code:
  1. Item {
  2. id: elementContainer
  3.  
  4. property QtObject theRelation
  5.  
  6. .... someHandler ...: {
  7. console.log (theRelation.fromObject())
  8. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. // .. QGraphicsObject *item = ... create QML component and instantiate an object ...
  2. item->setProperty ("theRelation", QVariant::fromValue ((QObject*) myRelation));
To copy to clipboard, switch view to plain text mode 

When the handler is called, it prints "undefined" as a result of the theRelation.fromObject() call.

-------------------------

I believe that a major source of confusion comes from the notion that there are three type systems involved:
  1. The traditional Qt/C++ types (QObject, using Q_DECLARE_METATYPE to make QVariant understand them)
  2. The types that the Javascript engine holds (Object, Function, Number, String etc.)
  3. The types that the QML engine understands (http://qt-project.org/doc/qt-4.8/qde...asictypes.html plus those registered with qmlRegisterType)


I implicitly assumes that each time a value crosses a boundary between C++, QML and Javascript an automatic conversion takes place. This conversion is probably the root of all evils, since a lot of pointer references are smashed to either some form of null (as in C++ to QML assignment via QDeclarativeItem::setProperty or QDeclarativeContext::setContextProperty) or undefined (like when a javascript expression tries to access a QML Component property).

After having used QML for some time now, I would simply just love a clarifying discussion on the type system. But to keep this post simple, I will stick to the simple question: Can QML gracefully handle pointers wrapped in QSharedPointer?

Best regards,
Rene Jensen