Solved Getting environment variables in QML
-
I saw help documentation https://doc.qt.io/qtcreator/creator-qml-modules-with-plugins.html
I hope to get environment variables (QML_PUPPET_MODE)in qml
But I have no way
Whether it can only get the environment variable on the c++ https://forum.qt.io/topic/80788/calling-environment-variables -
In your
main.cpp
file you can grab the env vars usingQProcessEnvironment
and add them as a context property...app.getEngine().rootContext()->setContextProperty("env", QProcessEnvironment::systemEnvironment().toStringList());
This is pretty raw and they will still need parsing so maybe you'd want to do that and then pass in a
QVariantMap
instead.In your
main.qml
you can try this by printingenv
in, say,Component.onCompleted
-
This post is deleted! -
@Martin-Wells said in Getting environment variables in QML:
But I have no way
Whether it can only get the environment variable on the c++Yes you can get this environment variable from C++
qgetenv("QML_PUPPET_MODE")
.
Then you can add it in the current QML context:
app.getEngine().rootContext()->setContextProperty("QML_PUPPET_MODE", qgetenv("QML_PUPPET_MODE"))
-
@kindid
Thank you this is a good idea.