Results 1 to 2 of 2

Thread: QSharedPointer / QWeakPointer in QML?

  1. #1
    Join Date
    May 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default QSharedPointer / QWeakPointer in QML?

    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

  2. #2
    Join Date
    May 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QSharedPointer / QWeakPointer in QML?

    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

Similar Threads

  1. QSharedPointer in a QVariant and back again
    By tomschuring in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2012, 23:02
  2. Replies: 1
    Last Post: 9th May 2011, 13:16
  3. emitting QSharedPointer
    By babu198649 in forum Newbie
    Replies: 11
    Last Post: 26th July 2010, 08:51
  4. Problem with QSharedPointer
    By weaver4 in forum Newbie
    Replies: 2
    Last Post: 19th April 2010, 14:22
  5. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 16:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.