Thursday, April 25, 2013

Using variables from project file (.pro) into C++ code

Have you ever wonder if any of the variables you have defined in your project file (.pro) can be used in your C++ source code? The answer is yes, and the process is straightforward.

Let's say you export the variables in the project file as macros (in this case you set the target name for the application), then using it from C++ is just simple.

In the project file (.pro)
TARGET = YourApp
TEMPLATE = app
DEFINES += TARGET=\\\"$TARGET\\\"

then you can use it in your C++ code like this:
// translation files are stored in resource file as YourApp_locale.qm
if translator.load(QString(TARGET) + "_" + locale, ":/languages") {
  ...
  ...
}

1 comment:

  1. Thanks! Your QString Argument before TARGET got me on the right track :-)

    ReplyDelete