Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??
Forum Updated to NodeBB v4.3 + New Features

Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 3 Posters 832 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gbhutra
    wrote on last edited by
    #1

    I have an app with QMdiArea as the central widget and each QMdiSubWindow has a QQuickWidget which displays a qml item. I have another QObject which gets initialized in each QQuickWidget whoose signals I want to capture in the Qml as shown. I was able to do that by creating a separate instance of QQmlEngine which creates a new QQmlContext my question is that is that the right way? Is it possible to set a context property to a local file?

    // SubWindow.cpp 
    ...
     // class SubWindow : public QMdiSubWindow
    SubWindow::SubWindow(QObject* parent, QQmlEngine* engine) : QMdiSubWindow(parent) {
      // creating a new engine as I need separate 'interface' property for each subwindow
      QQmlEngine* newContentEngine = new QQmlEngine(this);
      if (newContentEngine->rootContext() && engine && engine->rootContext()) {
       // copy 'colors' from the parent engine
        newContentEngine->rootContext()->setContextProperty(
            "colors", engine->rootContext()->contextProperty("colors"));
        Content* cont = new Content(this, newContentEngine);
        setWidget(cont);
      }
    }
    ...
    
    // ContentInterface.h
    ...
    class ContentInterface : public QObject {
    Q_OBJECT
    
    public:
     ContentInterface(QObject* parent);
    
    public signals:
     void dataChanged();
    }
    ...
    
    // Content.cpp
    ...
     // class Content : public QQuickWidget
    Content::Content(SubWindow* parent, QQmlEngine* contentEngine)  : QQuickWidget(contentEngine, parent) {
      ContentInterface* interface_ = new ContentInterface(this);
    
      // the rootContext is local to the new Engine created so works! 
      rootContext()->setContextProperty("interface", interface_);
      
      setResizeMode(QQuickWidget::SizeRootObjectToView);
      setSource(QUrl("qrc:/Content.qml"));
    }
    ...
    
    // Content.qml
    Rectangle {
    
    Connections  {
      target: interface
      onDataChanged : {
        ...
      }
    }
    
    raven-worxR 1 Reply Last reply
    0
    • G Gbhutra

      I have an app with QMdiArea as the central widget and each QMdiSubWindow has a QQuickWidget which displays a qml item. I have another QObject which gets initialized in each QQuickWidget whoose signals I want to capture in the Qml as shown. I was able to do that by creating a separate instance of QQmlEngine which creates a new QQmlContext my question is that is that the right way? Is it possible to set a context property to a local file?

      // SubWindow.cpp 
      ...
       // class SubWindow : public QMdiSubWindow
      SubWindow::SubWindow(QObject* parent, QQmlEngine* engine) : QMdiSubWindow(parent) {
        // creating a new engine as I need separate 'interface' property for each subwindow
        QQmlEngine* newContentEngine = new QQmlEngine(this);
        if (newContentEngine->rootContext() && engine && engine->rootContext()) {
         // copy 'colors' from the parent engine
          newContentEngine->rootContext()->setContextProperty(
              "colors", engine->rootContext()->contextProperty("colors"));
          Content* cont = new Content(this, newContentEngine);
          setWidget(cont);
        }
      }
      ...
      
      // ContentInterface.h
      ...
      class ContentInterface : public QObject {
      Q_OBJECT
      
      public:
       ContentInterface(QObject* parent);
      
      public signals:
       void dataChanged();
      }
      ...
      
      // Content.cpp
      ...
       // class Content : public QQuickWidget
      Content::Content(SubWindow* parent, QQmlEngine* contentEngine)  : QQuickWidget(contentEngine, parent) {
        ContentInterface* interface_ = new ContentInterface(this);
      
        // the rootContext is local to the new Engine created so works! 
        rootContext()->setContextProperty("interface", interface_);
        
        setResizeMode(QQuickWidget::SizeRootObjectToView);
        setSource(QUrl("qrc:/Content.qml"));
      }
      ...
      
      // Content.qml
      Rectangle {
      
      Connections  {
        target: interface
        onDataChanged : {
          ...
        }
      }
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Gbhutra said in Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??:

      Is it possible to set a context property to a local file?

      try QQmlComponent::create() with a newly created QQmlContext instance on which you have set the context property

      QQmlComponent comp(engine);
      comp.loadUrl(QUrl("qrc:/MyComp.qml"));
      QQmlContext* context = new QQmlContext(engine);
      context->setContextProperty(...);
      QObject* obj = comp.create(context);
      context->setParent(obj); // Note: not 100% sure if thats correct, but otherwise context would leak the memory
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • G Offline
        G Offline
        Gbhutra
        wrote on last edited by
        #3

        Thanks for the response.

        How can I embed a QQmlComponenet into a widget/QMdiSubWindow?

        raven-worxR 1 Reply Last reply
        0
        • Pradeep P NP Offline
          Pradeep P NP Offline
          Pradeep P N
          wrote on last edited by Pradeep P N
          #4

          Hi @Gbhutra
          Can you have look at Integrating QML Code with Existing Qt UI Code

          Good luck.

          Pradeep Nimbalkar.
          Upvote the answer(s) that helped you to solve the issue...
          Keep code clean.

          1 Reply Last reply
          2
          • G Gbhutra

            Thanks for the response.

            How can I embed a QQmlComponenet into a widget/QMdiSubWindow?

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @Gbhutra said in Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??:

            How can I embed a QQmlComponenet into a widget/QMdiSubWindow?

            QQuickWidget* w = new QQuickWIdget( engine ); // use the same shared engine instance
            w->setResizeMode( QQuickWidget::SizeRootObjectToView );
            w->setSource("qrc:/MyContainer.qml"); // where the MyContainer comp is a simple plain Item {}
            QQuickItem* root = w->rootObject();
            QQuickItem* item = qobject_cast<QQuickItem*>(obj); // obj comes from the code of my previous post (the object created from the QQmlComponent)
            item->setParentItem( root );
            QObject::connect(root, &QQuickItem::widthChanged, item, &QQuickItem::setWidth );
            QObject::connect(root, &QQuickItem::heightChanged, item, &QQuickItem::setHeight );
            

            @Pradeep-P-N said in Is it possible to setContextProperty local to a qml file displayed in a QQuickWidget??:

            Can you have look at Integrating QML Code with Existing Qt UI Code

            this link is related to QML1/Qt4, so not applicable

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved