Important: Please read the Qt Code of Conduct -

[Solved] Managing environment variables

  • Is there anything that Qt offers to manage application wide environment variables?
    I am talking about variables that the user controls inside of the application which aren't related to anything outside of the application.

    Right now all I do is having a QMap<QString, QString> inside my preferences class which I pass around all the objects that need to access the environment variables.

    Is there a better solution to this?

  • Since QCoreApplication derives from QObject, you could use it's property and setProperty functions. Then, you wouldn't need to constantly pass around a "properties" data structure, just include the QApplication header file and use the qApp global pointer.

  • Lifetime Qt Champion


    What kind of "environment variables" do you have in mind ? Application settings ?

  • You can define some system-wide macros in your .pro file, for example:
    DEFINES += TEST1=1
    DEFINES += TEST2=2

    Or you can just use normal c style globals.

    Or you can use c++ style singleton global class with your parameters.

    It may help to know what kind of data you want to use

  • @SGaist Not application settings. More stuff like library paths. The users can submit different libraries of components he is using inside of the application. He can use environment variables to give a base path for all libraries: $(LIB_ROOT)/library1.
    But this is all only internal in the application and has nothing to do with the actual system the application is running on. It is something the user controls to abstract paths and other things.

  • Lifetime Qt Champion

    For environment variables (like PATH if I understand you correctly), you can use qgetenv

  • @SGaist I am more talking about PATHs that are only used INSIDE of my application that have nothing to do with the external world. Environment variables that hold paths to binaries, libraries etc. Nothing the underlying operating system is aware of. It is only internal to my application.

  • Lifetime Qt Champion

    Then use a central object that provides these values. You can make it a singleton to ensure there's only one for your application.

Log in to reply