Results 1 to 2 of 2

Thread: How to get Object's pointer instantiated in QML engine

  1. #1
    Join Date
    Jul 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get Object's pointer instantiated in QML engine

    I have
    Qt Code:
    1. qmlRegisterType<DataModel>("one.DataModel", 1, 0, "DataModel");
    To copy to clipboard, switch view to plain text mode 
    and I instance this in QML
    Qt Code:
    1. DataModel {
    2. }
    To copy to clipboard, switch view to plain text mode 
    I need to get pointer to the istantiated objct in C++ side. BUT I do not want to use aproach with use of
    Qt Code:
    1. findChild()
    To copy to clipboard, switch view to plain text mode 
    .
    How to get a pointer?

    I have
    Qt Code:
    1. qmlRegisterType<DataModel>("one.DataModel", 1, 0, "DataModel");
    To copy to clipboard, switch view to plain text mode 
    and I instance this in QML
    Qt Code:
    1. DataModel {
    2. }
    To copy to clipboard, switch view to plain text mode 
    I need to get pointer to the istantiated objct in C++ side. BUT I do not want to use aproach with use of
    Qt Code:
    1. findChild()
    To copy to clipboard, switch view to plain text mode 
    .
    How to get a pointer?


    Added after 30 minutes:


    I got the solution
    Qt Code:
    1. QObject *dataModel = engine.rootObjects()[0]->property("refToMainModelForC").value<QObject*>();
    2. qDebug() << dataModel << "is address of data model";
    To copy to clipboard, switch view to plain text mode 

    in QML

    Qt Code:
    1. property var refToMainModelForC: treeModel
    2. DataModel {
    3. id: treeModel
    4. property var viewCurrentIndex: null
    5. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by kofr; 18th July 2016 at 12:04.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get Object's pointer instantiated in QML engine

    Or the model could register itself inside its C++ code.
    Or you call a register function in QML when you want to register a particular model.

    Both obviously a lot cleaner than making the C++ code depend in a specific property name on a specific object.

    And as always when "the need" to access a QML instantiated object arises: what do you need this for?

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 10th June 2016, 18:05
  2. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  3. static Object Vs Pointer
    By rajeshs in forum General Programming
    Replies: 4
    Last Post: 11th June 2008, 07:41
  4. Saving object pointer
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 11th January 2008, 11:53
  5. Pointer to ActiveX object in QT
    By json84 in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2007, 12: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.