PDA

View Full Version : QSharedPointer / QWeakPointer in QML?



centipede
21st May 2012, 12:46
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:


class LogicRelation : public QObject
{
Q_OBJECT
public slots:
....
QSharedPointer<LogicModel> fromObject ();
QSharedPointer<LogicModel> toObject ();
....
};
class LogicModel : public QObject
{
Q_OBJECT
...
};


Q_DECLARE_METATYPE( QSharedPointer<LogicModel> )
Q_DECLARE_METATYPE( QSharedPointer<LogicRelation> )



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:

Item {
id: elementContainer

property QtObject theRelation

.... someHandler ...: {
console.log (theRelation.fromObject())
}



// .. QGraphicsObject *item = ... create QML component and instantiate an object ...
item->setProperty ("theRelation", QVariant::fromValue ((QObject*) myRelation));

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:

The traditional Qt/C++ types (QObject, using Q_DECLARE_METATYPE to make QVariant understand them)
The types that the Javascript engine holds (Object, Function, Number, String etc.)
The types that the QML engine understands (http://qt-project.org/doc/qt-4.8/qdeclarativebasictypes.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

centipede
23rd May 2012, 10:25
Skip the request for a generic discussion. I'll take that to the qt-project.org developer mailing list. My question remains:

Can QML gracefully handle pointers wrapped in QSharedPointer?

Cheers