Important: Please read the Qt Code of Conduct -

Pass in static constants from c++ to qml

  • 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?

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

    ConstantObject 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.

Log in to reply