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

Environment variables

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.0k 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.
  • T Offline
    T Offline
    toxicate20
    wrote on last edited by
    #1

    Hey guys,

    So I need to run an non-Qt application lets call it 'App'. The options on which App runs are set via env Vars like export APP_VAR1="1". I implemented a DropDown to make a set of option choices which will need to call several of these export commands. I've failed to do this in Qt. I've been trying to work around it with a setup.sh script which is executed with system(./setup.sh) but it will only set the enviroment variable for the QtApp and not on the system. I've also used an implementation of QProcess but Im not sure if this was properly implemented.

    I would be super glad if anyone of you could tell me how to achieve this.

    Thanks in advance, tox20

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      Are you looking for QProcess + QProcessEnvironment ?

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • L Offline
        L Offline
        loladiro
        wrote on last edited by
        #3

        I assume you are launching your app from inside a Qt app?
        In that case:
        http://doc.qt.nokia.com/4.7/qprocess.html#setProcessEnvironment

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toxicate20
          wrote on last edited by
          #4

          Thanks so much guys, just what i was searching for. In fact i already had this thing put into my code but it didn't work out as it closed on start. This is caused by the fact that the memory for an app that you call via QProcess is allocated from the program stack. When the variable goes out of scope, the memory which was taken on the stack is freed. Therefore you need to alloate it in the heap. Here's how i did it.

          @
          void myQtApp::function()
          {
          QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
          env.insert("MYAPP_VAR1","value1");

           QProcess *proc; // pointer definition
           proc = new QProcess( this ); // memory allocation from heap, created with parent
           proc->setProcessEnvironment(env); // set environment variables
           proc->start("regedit.exe"); // start program
          

          }
          @

          Be are these env vars will not be available once the process is finished/closed.

          1 Reply Last reply
          0

          • Login

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