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. setContextProperty: Best practice for having the rootContext
Forum Updated to NodeBB v4.3 + New Features

setContextProperty: Best practice for having the rootContext

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 328 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.
  • M Offline
    M Offline
    maxwell31
    wrote on last edited by
    #1

    Hi,

    I use a QAbstractListItemModel which I want to have as a member of a class. In order to have it available in QML, I need to use setContextProperty.

    QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel", QVariant::fromValue(dataList));
    

    However, I wonder what the best practice is for having the rootContext available in my class. Should I just give it as argument to the constructor? Or is it better to have a function which returns my model, so that I can set the context property in a place where I have the rootContext already (e.g. the main function or a gui handler function)?

    Or does it make sense to make the rootContext a global variable?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It's not clear why you want to have access to the QML context from within your C++ class.

      It sounds rather like a bad idea.

      What does that class do except containing that model you mentioned ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

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