1
votes

I want to add a path to my translations folder in the .pro file, which can be accessed from C++ / QML parts as well as used inside the .pro file.

I came across the DEFINES+= function and made it work with an example for the number PI. This define can be called from C++ using qDebug()<

Now i have a translations folder two directorys above the .pro file which is called translations. Therefor i tried to use something like this:

DEFINES += "TRANSPATH=\"../../translations\""

But when I try to access it via qDebug i get the errors - expected primary expression and - expected unqualified id before "." token

In my .pro file I want to access the TRANSPATH as well, but using it like

 TRANSLATIONS += \
     $$(TRANSPATH)/test_TEST.ts \
     ../../translations/de_DE.ts \
     ../../translations/zh_CN.ts
 }

only leads to this error:

  Updating '../../../../../../../test_TEST.ts'...
      Found 63 source text(s) (63 new and 0 already existing)
  Cannot create /test_TEST.ts: Zugriff verweigert  
  Updating '../../translations/de_DE.ts'...
      Found 63 source text(s) (0 new and 63 already existing)

I tried to find other examples online but haven´t found anything helpfull. In the TRANSLATIONS += part i had changed the wording to:

  TRANSPATH/test_TEST.ts
  {TRANSPATH}/test_TEST.ts 
  $$TRANSPATH/test_TEST.ts
  $${TRANSPATH}/test_TEST.ts

but nothing worked. This is the first time I´m trying to DEFINE something, maybe I´m doing it wrong? Please help

Example code / .pro file:

 # this file will be loaded from the main import path
  MAIN_QML_FILE = main.qml
  INCLUDEPATH += ./Plugins
  INCLUDEPATH += ./qml
  DEFINES += "PI=\"3.1415926\""
  DEFINES += "TRANSPATH=\"../../translations\""
  QT += core
  # this is only seen by the linguist tools (lupdate)
  lupdate_only{
  SOURCES = \
      ../../qml/Widgets/SomeFiles/*.qml

  TRANSLATIONS += \
      $$(TRANSPATH)/test_TEST.ts \
      ../../translations/de_DE.ts \
      ../../translations/zh_CN.ts
  }

TRANSPATH should lead to the same folder as the ../../translations/de_DE.ts path does. The path would be reused from C++ for a custom QTranslator object.

1

1 Answers

2
votes

First, this error from qmake:

Cannot create /test_TEST.ts: Zugriff verweigert

comes from here:

$$(TRANSPATH)/test_TEST.ts

You're referencing an undefined variable, i.e. TRANSPATH. When you do this:

DEFINES += "TRANSPATH=\"../../translations\""

you're not defining a variable: you're appending a define to the compiler command line, using -D flag (you can check this in the compilation output pane, in creator).

So, just have a qmake variable:

TRANSPATH = ../../translations

Now you can use it elsewhere in your pro file, e.g.

  TRANSLATIONS += \
      $$(TRANSPATH)/test_TEST.ts \
      ../../translations/de_DE.ts \
      ../../translations/zh_CN.ts
  }

You can use it in DEFINES, too, but take care of escaping:

DEFINES += "TRANSPATH=\\\"$$TRANSPATH\\\""

In your compiler out you will find

-DTRANSPATH=\"..\..\translations\"

along with the other flags.

Now you can safely do

qDebug() << TRANSPATH; 

in your source code.