Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use qmake variable in cpp file
Qt 6.11 is out! See what's new in the release blog

Use qmake variable in cpp file

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 11.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by A Former User
    #1

    Hello,
    in my .pro file I specify

    VERSION = 1.0.0
    QMAKE_TARGET_PRODUCT = "MyApp"
    QMAKE_TARGET_COMPANY = "Company"
    QMAKE_TARGET_COPYRIGHT = "Copyright (c) by Company"
    

    How can I access these variables in my cpp files? I don't want to specify this information on multiple places.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      In *pro file:

      DEFINES += MYSTRING='\\"Hello\ World\\"'
      

      In C++ code:

      qDebug() << MYSTRING;
      
      1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        @Wieland Unfortunately this is not gonna work if you want to use it in both qmake variables and defines for c++ e.g.

        QMAKE_TARGET_COPYRIGHT = "Copyright (c) by Company"
        DEFINES += APP_COPYRIGHT='\\"$${QMAKE_TARGET_COPYRIGHT}\\"'
        

        This will fail because of the spaces and you will get a weird error about a newline in constant or something similar.
        To make this work correctly I found a sorta jolly slash fest like this does the job:

        VERSION = 1.0.1
        QMAKE_TARGET_PRODUCT = "MyApp"
        QMAKE_TARGET_COMPANY = "Company"
        QMAKE_TARGET_COPYRIGHT = "Copyright (c) by Company"
        
        DEFINES += APP_VERSION=\"\\\"$${VERSION}\\\"\" \
                   APP_PRODUCT=\"\\\"$${QMAKE_TARGET_PRODUCT}\\\"\" \
                   APP_COMPANY=\"\\\"$${QMAKE_TARGET_COMPANY}\\\"\" \
                   APP_COPYRIGHT=\"\\\"$${QMAKE_TARGET_COPYRIGHT}\\\"\"
        

        Note: There should be two $ next to each other but for whatever reason the forum formatting is eating it :/

        And then you can use them like text macros:

        qDebug() << APP_VERSION;
        qDebug() << APP_PRODUCT;
        qDebug() << APP_COMPANY;
        qDebug() << APP_COPYRIGHT;
        
        1 Reply Last reply
        1
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @Chris-Kawa said:

          jolly slash fest

          Looks funny, indeed ^_^

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by SGaist
            #5

            Hi,

            Another possibility is to generate a file e.g. a header file that contains these constants:

            myproject.pro

            INCLUDEPATH += $$OUT_PWD/include
            info_file.input = $$PWD/info.h.in
            info_file.output = $$OUT_PWD/include/info.h
            
            QMAKE_SUBSTITUTES += info_file
            OTHER_FILES += \
                info.h.in
            

            info.h.in

            #ifndef INFO_H
            #define INFO_H
            
            #include <QString>
            
            static const QString AppVersion = QStringLiteral(\"$$APP_VERSION\");
            static const QString AppProduct = QStringLiteral(\"$$APP_PRODUCT\");
            static const QString AppCompany = QStringLiteral(\"$$APP_COMPANY\");
            static const QString AppCopyRight = QStringLiteral(\"$$APP_COPYRIGHT\");
            #endif // INFO_H
            

            [edit: removed double dollars workaround, renderer is now working fine SGaist]

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved