Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved] Passing #defines between the .pro and the C++ code
Forum Updated to NodeBB v4.3 + New Features

[Solved] Passing #defines between the .pro and the C++ code

Scheduled Pinned Locked Moved Mobile and Embedded
7 Posts 2 Posters 13.4k Views 1 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.
  • AlicemirrorA Offline
    AlicemirrorA Offline
    Alicemirror
    wrote on last edited by
    #1

    Hi to all,

    I need to set some defines (i.e. the application version, but not only) in the .pro file of the Qt Creator. Then I need to get these definitions exposing them as read-only properties in the QML documents.

    Currently I have adopted this tecnique:

    project.pro
    @
    DEFINES += APP_VERSION=$$VERSION
    DEFINES += FILE_TYPE=Data test

    also tried with "Data test" with no difference...

    @

    class.cpp
    To be simple, suppose that I should assign to a QString the version and to another QString the file type. Thus I use the following lines of code:
    @
    QString m_appver, m_filetype;

    m_appver.append(APP_VERSION);
    m_filetype.append(FILE_TYPE);
    @

    When in the Qt Creator editor I start typing APP_... the autocompletion function shows me the full define name, as with the FILE_... This means - I suppose - that as I expect the .pro defines are visible inside the C++ code. But despite this assignation that is compliled without problems, as a matter of fact the property does not reports any value.

    Some ideas of what is that I can have omitted ?

    Enrico Miglino (aka Alicemirror)
    Balearic Dynamics
    Islas Baleares, Ibiza (Spain)
    www.balearicdynamics.com

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      Have a look at the produced command line. Remember that this define has to not only survive through qmake, but also through your Makefile and the compiler invocation.

      It is well possible you have to escape the quotes
      @DEFINES += FILE_TYPE=\"Data test\"@

      I've even needed to use
      @\\"Some text\\"@
      at some point.

      The version may well have to be quoted as well.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • AlicemirrorA Offline
        AlicemirrorA Offline
        Alicemirror
        wrote on last edited by
        #3

        Thank you Franzk, I was also suspecting something like this. The question at this point is that I have not clear how doest the escape works.

        I can understand that if for every passage it is lost a escaping, a double escape may have sense, but - by intuition - what I expect is something like " = the escaping and \ " double escaping? So when you use \\" means the escape of the escape of the escape ?

        Is there a method to focus this or is to be tried ?

        Thank you anyway, the concept of escaping I had missed totallly before.

        Enrico Miglino (aka Alicemirror)
        Balearic Dynamics
        Islas Baleares, Ibiza (Spain)
        www.balearicdynamics.com

        1 Reply Last reply
        0
        • AlicemirrorA Offline
          AlicemirrorA Offline
          Alicemirror
          wrote on last edited by
          #4

          Franzk: I have done a more detailed test and the result is partially correct. Remain the problem that I can't see the define in the QML. Following are code snippets:

          In project.pro
          @

          DEFINES += APP_VERSION=\$$VERSION\\
          APP_NAME=\$$TARGET\
          @

          Then in myclass.h
          @
          // In the class definition the two macro for the readonly property
          Q_PROPERTY(QString appVersion READ appVersion)
          Q_PROPERTY(QString appName READ appName)

          public:
          QString appName() const;
          QString appVersion() const;

          private:
          QString m_appname, m_appversion;
          @

          And in myclass.cpp
          @
          MyClass::MyClass(QObject *parent) : QObject(parent)
          {
          m_appversion.append(APP_VERSION);
          m_appname.append(APP_NAME);

          qDebug() << "MyClass::MyClass(QObject *parent) initialized m_appname  " << m_appname << " m_appversion " << m_appversion;
          

          }

          // And obviously

          QString MyClass::appName() const
          {
          return m_appname;
          }

          QString MyClass::appVersion() const
          {
          return m_appversion;
          }
          @

          In main.cpp the class is registered correctly (all the other properties and methods are working)
          @
          int main(int argc, char *argv[])
          {
          QApplication app(argc, argv);

          // Register MyClass for QML usage
          qmlRegisterType<MyClass>("qtc.data", 1,0, "MyClass");
          

          ...
          @

          Finally in the QML MainPage.qml
          @
          // The class is instantiated correctly and all works fine
          MyClass {
          id: dataParameters

             // various settings ...
          }
          
          Component.onCompleted: {
              console.debug("*** DataItem finished loading: assigned the objects to the loaders.")
              console.debug("*** Name: " + MyClass.appName);
              console.debug("*** Version: " + MyClass.appVersion);
          }
          

          @

          The console output shows the value in the C++ source (at initialization) while these two properties - managed as many other in the same class - are always undefined

          console log
          @
          MyClass::MyClass(QObject *parent) initialized m_appname "Application Name" m_appversion "0.4.9"
          @
          both of these are correct accordingly with the defines in the project file
          @
          *** Name: undefined
          *** Version: undefined
          @
          The same when logged from QML are always undefined.

          Any idea ? Maybe it is not possible for some rason?

          Enrico Miglino (aka Alicemirror)
          Balearic Dynamics
          Islas Baleares, Ibiza (Spain)
          www.balearicdynamics.com

          1 Reply Last reply
          0
          • AlicemirrorA Offline
            AlicemirrorA Offline
            Alicemirror
            wrote on last edited by
            #5

            Solved (it was a bug of myself).

            All was correct in the previous example and investigating step by step I saw that the error was at line 10 and 11 of the MainPage.qml snippet:

            I was using MyClass.<propertyname> instead of the class id. Thus the element name is not instantiated nor loaded in the applicaiton. To work propertly it should be:
            @
            Component.onCompleted: {
            console.debug("*** DataItem finished loading: assigned the objects to the loaders.")
            console.debug("*** Name: " + dataParameters.appName);
            console.debug("*** Version: " + dataParameters.appVersion);
            @

            Many thanks for the suggestion because this is the solution for passing data.

            :)

            Enrico Miglino (aka Alicemirror)
            Balearic Dynamics
            Islas Baleares, Ibiza (Spain)
            www.balearicdynamics.com

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

              [quote author="Alicemirror" date="1317675111"]
              I can understand that if for every passage it is lost a escaping, a double escape may have sense, but - by intuition - what I expect is something like " = the escaping and \ " double escaping? So when you use \\" means the escape of the escape of the escape ?[/quote]

              What you need is "sometext" to appear on the command line. You need to escape the " for the Makefile, because it will not appear on the command line: ". The Makefile will contain "sometext". However, if you pass this from qmake to the Makefile, the " will end up being " again, so you have to escape both characters in the project file: \", resulting in \"sometext\"

              Good to see you got everything working.

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • AlicemirrorA Offline
                AlicemirrorA Offline
                Alicemirror
                wrote on last edited by
                #7

                Yes Franzk, many thanks. It works and your advices were essential :)

                Enrico Miglino (aka Alicemirror)
                Balearic Dynamics
                Islas Baleares, Ibiza (Spain)
                www.balearicdynamics.com

                1 Reply Last reply
                0

                • Login

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