Important: Please read the Qt Code of Conduct -

Environment variables

  • Hi,

    My Qt application uses multiple text files. I want my application executable to be placed in a directory that is specified by the user and the text files can be placed in some other directory.
    How can I control this using flags e.g. qmake -executable_director -text_files_directory and then use text_files_directory in my code to read the text files? Basically I want to give user the privilege to specify where the executable is placed and where the text files should be placed while building/installing the application. I wish I could be more clearer :(

  • Lifetime Qt Champion


    Do you mean you want run time and build time environment variables ?

  • Thanks for your reply. Let me explain again please.

    I made an application using QT creator and its running fine. When I run the application it does following

    • list itemReads some text files {File1,File2...,FileN} from the directory MY_DIR=/home/develop/Documents/

    • list itemExecutable is placed in EXE_DIR=default directory of project

    In my code the variable MY_DIR is set to /home/develop/Documents/. Now I want to allow the user to set the value of MY_DIR while building/installing the application without QT creator and use that value in my source code.
    So, I want to install this application on a PC that has no QT creator but QT installed. I want to make a script that can be used to

    • list itembuild\install the application

    • list itemSet the value of MY_DIR and EXE_DIR.

    • list itemCopies text files {File1,File2...,FileN} from source directory to MY_DIR

    • list itemPlaces the executable in EXE_DIR.

    Some things like this

    qmake -MY_DIR=/usr/local/lib/text_files -EXE_DIR=/usr/bin

    In my source code

    Qsettings* genSettings;
    genSettings = new Qsettings(MY_DIR+"/File1", QSettings::IniFormat);

  • Qt Champions 2019

    @UHaider You should not hard code such paths in your code! Especially not if your users are not going to build the software by themselves.
    Such configuration is usually done using config files on UNIX/Linux or registry on Windows. For that you should take a look at QSettings
    That means this MY_DIR would be just a setting which can be changed by user and which will be read by your application at runtime.

  • Lifetime Qt Champion

    Normally the location of the installed exe and location of data files are selected in the installer.
    You can also offer a menu to point to a new location in the app for the data files.
    And as @jsulm says, it should not be compiled into the .exe but kept as a setting externally.

  • @jsulm thanks for your reply. I think I was not clear. The users are going to build the software themselves. That is why I want to pass it like an argument to qmake and then read that value in source code.

  • Qt Champions 2019

    @UHaider Something like this:

    qmake DEFINES+='MY_DIR "PATH"'


  • Lifetime Qt Champion

    Even if they build it themselves, hardcoding such values is no a good idea. QStandardPaths, QCoreApplication::applicationDirPath and QCommandLineParser. are better suited to get the files you want from when running your application.

    Install target would be a good place to say where you want your application to be installed.

Log in to reply