Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pass in static constants from c++ to qml
Forum Updated to NodeBB v4.3 + New Features

Pass in static constants from c++ to qml

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 327 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.
  • Y Offline
    Y Offline
    yeehaw
    wrote on last edited by
    #1

    I have a long list of QString constants that I would like to access from both c++ and QML. These constants are static and will never change in time.
    Ideally I'm looking to use the same source and export from one engine to the other (for scalability and maintenance reasons).

    I tried creating a QObject derived class that has const static QString members, with corresponding MEMBER properties. I registered this type as uncreatable to conform to the static nature of the problem, but it seems properties are inaccessible in QML through an uncreatable type.

    Is there any other correct, safe way with low overhead to access these const QStrings in QML?
    Through a Q_NAMESPACE perhaps? Accessing constants from a Q_NAMESPACE in QML doesn't seem doable as well.
    Any ideas?

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Just instance your object somewhere in main or function called from main.

      ConstantObject constobj;
      engine->rootContext()->setContextObject(&constobj);
      

      Set QObject* as contextObject.
      The members are now values that appear as standalone values. I would just do q_property with read though. Member I think implies being able to be modified.

      C++ is a perfectly valid school of magic.

      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