Results 1 to 3 of 3

Thread: C++ pointer to Qml - is this possible?!?

  1. #1
    Join Date
    Feb 2015
    Location
    Poland
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Exclamation C++ pointer to Qml - is this possible?!?

    Hi

    [[[ I waste half day for googling and trying different code. So I am not lazy when I am asking. ]]]

    I have simple question whether is this possible:
    Create and feed QSqlQueryModel in C++ and send it (whatever way) to Qml?
    If you claim yes, then please attach prof code...

    And I am not interested with expose some alone object to Qt (I have no problem with this - I can use lEngine.rootContext()->setContextProperty from main()) But I want send any number of C++ models to Qml (different models to different Qml objects).
    I have problem with passing parameters to functions and to property (pointers as parameters).

    Thank you and best regards
    Szyk Cech
    Last edited by Szyk; 16th May 2017 at 20:10.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: C++ pointer to Qml - is this possible?!?

    I admit that QML is not my strong point.
    However, since you want to expose a C++ pointer to QML, I don't know if there is any other option but to expose it via a Q_PROPERTY.
    So you will have some sort of a C++ QObject class that has a property which is your model pointer and then:
    Qt Code:
    1. class MyModelExposer : public QObject
    2. {
    3. ....
    4. Q_PROPETRY(QSqlQueryModel* sqlQueryModel read sqlQueryModel write setSqlQueryModel NOTIFY onSqlQueryModelChnaged)
    5. ...
    6.  
    7. pivate:
    8. QSqlQueryModel* sqlQueryModel;
    9. };
    10.  
    11.  
    12. //Somewhere in your C++ code:
    13. qmlRegisterUncreatableType<MyModelExposer>("bar.foo", 1, 0, "MyModelExposer", "");
    14. ...
    15.  
    16. MyModelExposer modelExposerInstance;
    17. ...
    18. ..
    19.  
    20. engine.rootContext()->setContextProperty("modelExposer", &modelExposerInstance);
    To copy to clipboard, switch view to plain text mode 

    And then in QML:
    Qt Code:
    1. SomeModelConsumer {
    2. model: modelExposer.model
    3. }
    To copy to clipboard, switch view to plain text mode 

    Again, may be there are sothers here who are better at QML and can offer a better solution and/or correct my answer if it needs correction.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2015
    Location
    Poland
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Exclamation Re: C++ pointer to Qml - is this possible?!?

    You won't notice my mention about setContextProperty... So I am not satisfy with your answer.
    Correct answer is that: Yes this is possible in that way:
    In order to use pointers variable in Qml you will have to do following steps:
    1. Register your class in order to make it understandable to QVariant:
    qRegisterMetaType<QSqlQueryModel*>("QSqlQueryModel *");
    2. Register your class in Qml:
    qmlRegisterType<QSqlQueryModel>("com.shomething", 1, 0, "QSqlQueryModel");
    3. Create your variable:
    QmlTableModel* lModel2 = new QmlTableModel(this);
    4. Get object contains your QSqlQueryModel* property from QQmlApplicationEngine:
    QObject* lCallbackObject = findObject(lCallbackObjectName);
    5. Set property:
    lCallbackObject->setProperty("mModel", QVariant::fromValue(lModel2));

    Essential in this procedure is QVariant::fromValue - qVariantFromValue will not work.
    I didn't check whether the same works with function calls from C++ to Qml, but it should.
    Last edited by Szyk; 17th May 2017 at 19:49.

Similar Threads

  1. Replies: 3
    Last Post: 23rd May 2015, 14:57
  2. Replies: 1
    Last Post: 4th December 2010, 18:20
  3. this pointer
    By salmanmanekia in forum General Programming
    Replies: 3
    Last Post: 11th June 2010, 09:43
  4. Pointer to Pointer
    By ^NyAw^ in forum General Programming
    Replies: 5
    Last Post: 25th April 2009, 09:00
  5. pointer
    By mickey in forum General Programming
    Replies: 4
    Last Post: 1st September 2006, 01:42

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.