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. Getting environment variables in QML

Getting environment variables in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 3.8k Views
  • 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
    Martin.Wells
    wrote on last edited by
    #1

    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
    7df34e62-8c4b-4461-a301-012e6dac5818-image.png
    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

    kindidK KroMignonK 2 Replies Last reply
    0
    • M Martin.Wells

      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
      7df34e62-8c4b-4461-a301-012e6dac5818-image.png
      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

      kindidK Offline
      kindidK Offline
      kindid
      wrote on last edited by
      #2

      @Martin-Wells

      In your main.cpp file you can grab the env vars using QProcessEnvironment 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 printing env in, say, Component.onCompleted

      M 1 Reply Last reply
      2
      • M Offline
        M Offline
        Martin.Wells
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M Martin.Wells

          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
          7df34e62-8c4b-4461-a301-012e6dac5818-image.png
          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

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #4

          @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"))

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          3
          • kindidK kindid

            @Martin-Wells

            In your main.cpp file you can grab the env vars using QProcessEnvironment 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 printing env in, say, Component.onCompleted

            M Offline
            M Offline
            Martin.Wells
            wrote on last edited by
            #5

            @kindid
            Thank you this is a good idea.

            1 Reply Last reply
            1

            • Login

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