Results 1 to 8 of 8

Thread: Load a portion of UI from a plugin QML (dynamic GUI) and expose C++ models to it

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Load a portion of UI from a plugin QML (dynamic GUI) and expose C++ models to it

    Quote Originally Posted by anda_skoa View Post
    You could register a QML singleton type using a plugin specific namespace and then let the QML code "instantiate" the model.

    Cheers,
    _
    Thanks for your time on this. Do you mean using qmlRegisterType<X>("com.mycompany.X", 1, 0, "X")?

  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: Load a portion of UI from a plugin QML (dynamic GUI) and expose C++ models to it

    I meant qmlRegisterSingletonType()

    However, there might be another option that is closer to what you tried.
    I assume you are using QQmlComponent to create the UI. You can pass a context to its create method.
    E.g. something like this (not tested)
    Qt Code:
    1. QQmlContext *pluginContext = new QQmlContext(view.engine());
    2. pluginContext->setContextProperty("model", plugin->getModel());
    3.  
    4. QQmlComponent component(view.engine(), plugin->getQMLPath());
    5. component.create(pluginContext);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    gmanish (3rd June 2014)

  4. #3
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Load a portion of UI from a plugin QML (dynamic GUI) and expose C++ models to it

    Quote Originally Posted by anda_skoa View Post
    I meant qmlRegisterSingletonType()
    However, there might be another option that is closer to what you tried.
    I assume you are using QQmlComponent to create the UI. You can pass a context to its create method.
    _
    I'm using the following to create the main UI (i.e. loading main.qml). It is in this view that I want to load the plugins QML components:

    Qt Code:
    1. QGuiApplication app(argc, argv);
    2. QQuickView viewer;
    3.  
    4. viewer.setSource(QUrl("qrc:///..."));
    5. viewer.showExpanded();
    6. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    Also, I found this thread that seems pertinent to my problem. However, I cannot fully comprehend it. Specifically, what are window1, window2 & window3 in the given solution.

    How do I create my main UI using QQmlComponent and later add the QQmlComponent created using the method you specify to the main UI's QQmlComponent? I found a possible implementation here. But when I run it, I get a crash because:

    Qt Code:
    1. QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    To copy to clipboard, switch view to plain text mode 

    evaluates to window = NULL
    Last edited by gmanish; 3rd June 2014 at 20:47.

  5. #4
    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: Load a portion of UI from a plugin QML (dynamic GUI) and expose C++ models to it

    Quote Originally Posted by gmanish View Post
    Qt Code:
    1. QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    To copy to clipboard, switch view to plain text mode 

    evaluates to window = NULL
    Is your top level item a Window element? If not, cast it to whatever you are using, e.g. QQuickItem

    Cheers,
    _

  6. #5
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Load a portion of UI from a plugin QML (dynamic GUI) and expose C++ models to it

    Mostly for future visitors, I did manage to get this working. Following is a tiny code fragment that highlights the solution:

    Qt Code:
    1. QQmlContext* pluginContext = new QQmlContext(mView->engine());
    2. QmlComponent* component(new QQmlComponent(mView->engine(), "/PATH/TO/QML.qml"));
    3. QObject* compRoot = NULL;
    4.  
    5. pluginContext->setContextProperty("model", myModelObject);
    6. compRoot = component->create(pluginContext);
    7. if (component->isReady()) {
    8. QQuickItem* item = qobject_cast<QQuickItem*>(compRoot);
    9. QPointF point(posHelper.getLeft(), posHelper.getTop());
    10. QSizeF size(posHelper.getWidth(), posHelper.getHeight());
    11.  
    12. item->setParentItem(mRoot); // The item pointer obtained through QQuickView.rootObject
    13. item->setPosition(point);
    14. item->setSize(size);
    15.  
    16. qDebug() << "Card (" << posHelper.getCurRow() << ", " << posHelper.getCurCol() << "): " <<
    17. "(" << point.x() << ", " << point.y() << ") - " <<
    18. "(" << size.width() << ", " << size.height() << ")";
    19. return component;
    20. } else {
    21. qWarning() << "Failed to create component from C++:" << component->errorString();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    The thing that did the trick was:

    Qt Code:
    1. item->setParentItem(mRoot)
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to gmanish for this useful post:

    anda_skoa (17th June 2014)

Similar Threads

  1. Link to plugin as dynamic lib in another plugin
    By alizadeh91 in forum Installation and Deployment
    Replies: 5
    Last Post: 16th December 2012, 22:16
  2. Replies: 1
    Last Post: 23rd June 2011, 23:09
  3. Dynamic load QT Plugin - symbol lookup error
    By qlands in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2010, 19:37
  4. Load objects from dynamic library
    By Trok in forum Qt Programming
    Replies: 10
    Last Post: 17th July 2009, 20:04

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
  •  
Qt is a trademark of The Qt Company.