Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Qt, what are the different ways to set an environment variable and what are the pros & cons?

Specifically:

  • What are all the different ways I can set an environment variable? I know about qputenv. What about qmake and the .pro file? What about Qt Creator build settings? What about commands like QGuiApplication::setAttribute(Qt::AA_UseOpenGLES);? Are there other ways too? Please include any restrictions (e.g. qputenv must be used before instantiating the QApplication within main.cpp)
  • What are the pros and cons of each approach?
  • For each approach, can you give an example setting the variable to:
    • an integer
    • a string
    • another environment variable
like image 432
Paul Masri-Stone Avatar asked Sep 03 '25 15:09

Paul Masri-Stone


1 Answers

There's a lot of confusion here... qmake and pro files can set environment variables for the compilation process, qputenv for the application itself (and its children).

setAttribute isn't even about environment variables - it's about some particular settings of the QGuiApplication.

Given that they affect completely different things, there's no way to talk about pros and cons - we are comparing apples to oranges.

As for setting environment variables to particular types, that's a question without much meaning - environment variables are just a dictionary of strings. If you want to set it to an integer, you'll have to convert it to a string first with whatever Qt/C/C++ method you prefer. As for the setAttribute, again it's a completely different thing, it just manipulates flags, so talking about setting a string or an integer has no meaning here.

like image 162
Matteo Italia Avatar answered Sep 05 '25 15:09

Matteo Italia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!