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
Forum Updated to NodeBB v4.3 + New Features

Getting environment variables in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 3.2k 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 10 Jun 2021, 06:53 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

    K K 2 Replies Last reply 10 Jun 2021, 19:38
    0
    • M Martin.Wells
      10 Jun 2021, 06:53

      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

      K Offline
      K Offline
      kindid
      wrote on 10 Jun 2021, 19:38 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 11 Jun 2021, 06:35
      2
      • M Offline
        M Offline
        Martin.Wells
        wrote on 11 Jun 2021, 05:21 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M Martin.Wells
          10 Jun 2021, 06:53

          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

          K Offline
          K Offline
          KroMignon
          wrote on 11 Jun 2021, 05:44 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
          • K kindid
            10 Jun 2021, 19:38

            @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 11 Jun 2021, 06:35 last edited by
            #5

            @kindid
            Thank you this is a good idea.

            1 Reply Last reply
            1

            1/5

            10 Jun 2021, 06:53

            • Login

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