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. DEFINES in QT Creator

DEFINES in QT Creator

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 8.4k 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.
  • U Offline
    U Offline
    UHaider
    wrote on last edited by
    #1

    Hi,

    I am trying to pass value of a DEFINES from Qt creator to source code. I am passing following line in Qt Creator

    DEFINES+=MY_DIR=\"username\"
    Click on link Image.

    But when I use following line of code

    qDebug () << MY_DIR;
    

    I get

    error: C2065: 'username': undeclared identifier.

    I also tried

    DEFINES+='MY_DIR "username"' but it gives some other error. If I set the value to an integer it works fine like DEFINES+=MY_DIR=541. But setting the value to string is not working :(

    What is wrong here?

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

      Hi,

      IIRC, the windows shell needs something a bit more complex to handle quoted string.

      However, are you sure that MY_DIR should be a define ? From what you wrote it seems it should rather be something that you can change at run time, no ?

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

      U 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi,

        IIRC, the windows shell needs something a bit more complex to handle quoted string.

        However, are you sure that MY_DIR should be a define ? From what you wrote it seems it should rather be something that you can change at run time, no ?

        U Offline
        U Offline
        UHaider
        wrote on last edited by
        #3

        @SGaist Thanks for your time. No, not at run time. I only need to set while building\installing the application. It is not working on Linux either :(

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

          Can you give more details about what you do with that define in your application ?

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

          U 1 Reply Last reply
          1
          • SGaistS SGaist

            Can you give more details about what you do with that define in your application ?

            U Offline
            U Offline
            UHaider
            wrote on last edited by
            #5

            @SGaist the define contains the path to a folder. In my Qt application I am using the define to read files in that folder and display the text of the files read. I want this to be a define so that user can pass this as an argument to qmake while building\installing the application.

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

              Well, that sounds rather like something that your user should either configure as command line parameter or read from a configuration file. Otherwise if he wants to change that folder for whatever reason he may had, he won't need to re-build/re-install the application.

              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
              0
              • U Offline
                U Offline
                UHaider
                wrote on last edited by
                #7

                Yes you are right but user cannot change the location of the folder once application is installed so a define can be used in this scenario. But I am unable to do it. Can you please give any example for doing it?

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

                  Why can't he change that ?

                  Try with:

                  MY_DIR_STR = '\\"$${ZE_DIR}\\"'                                   
                  DEFINES += MY_DIR=\"$${MY_DIR_STR}\"
                  
                  message($$DEFINES)
                  

                  Call qmake like that

                  qmake ZE_DIR=whatever
                  

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

                  U 1 Reply Last reply
                  3
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    I did
                    DEFINES+=PROJECT_LOCATION="$$shell_path($$PRO_FILE_PWD)"
                    in my profile to know where project is.

                    To use it as a string i had to do the following

                    #define _STR(x) #x
                    #define STR(x) _STR(x)

                    int main(int argc, char** argv) {
                    const char* ProjLocation = STR(PROJECT_LOCATION);

                    without the stringify macro (STR) it would see the actual path and give compile errors.

                    1 Reply Last reply
                    1
                    • SGaistS SGaist

                      Why can't he change that ?

                      Try with:

                      MY_DIR_STR = '\\"$${ZE_DIR}\\"'                                   
                      DEFINES += MY_DIR=\"$${MY_DIR_STR}\"
                      
                      message($$DEFINES)
                      

                      Call qmake like that

                      qmake ZE_DIR=whatever
                      
                      U Offline
                      U Offline
                      UHaider
                      wrote on last edited by
                      #10

                      @SGaist Thanks for your reply. It is working :)
                      It is a kind of restriction on user I know it is not good practice.

                      1 Reply Last reply
                      1

                      • Login

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