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. QmlRegisterType template fails if used in macro
Qt 6.11 is out! See what's new in the release blog

QmlRegisterType template fails if used in macro

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.7k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I recently posted the same question on "StackOverflow":http://stackoverflow.com/questions/26384974/qmlregistertype-template-fails-if-used-in-macro but sadly, there are not a lot of Qt experts on that forum.

    The question is the following:

    I'm currently working with Qt and qml files; many of the qml sources that I'm using takes advantage of the hability to publish C++ classes into the qml runtime with the qmlRegisterType template:

    @int main(int argc, char *argv[])
    {
    QApplication application(argc,args);

    qmlRegisterType<Component1>("CompanyName", 1, 0, "Component1");
    qmlRegisterType<Component2>("CompanyName", 1, 0, "Component2");
    ...
    lots of components
    ...
    qmlRegisterType<ComponentN>("CompanyName", 1, 0, "ComponentN");
    
    return application.exec&#40;&#41;;
    

    }@

    The amount of C++ types registered on qml is pretty high, and the registering is kind of cumbersome so I've decided to put the registering into a macro due to the fact that many of the data is repeated on all the registering process, the macro looks like the following:

    @#define QMLTYPE(T) qmlRegisterType<T>("CompanyName", 1, 0, #T);@

    And you can guess how is it intended to be used:

    @int main(int argc, char *argv[])
    {
    QApplication application(argc,args);

    QMLTYPE(Component1);
    QMLTYPE(Component2);
    ...
    lots of components
    ...
    QMLTYPE(ComponentN);
    
    return application.exec&#40;&#41;;
    

    }@

    AFAIK after expanding this macro the code should look like the first posted on this question so, the functionality should remain unaltered. But after launching the macro-version of the program, an error shows up:

    bq. Component1 is not a type

    It seems that the qml runtime is unable to found the types registered, as if the macro is failing or preventing the Qt voodoo to publish the type. Reverting to the no-macro-version the application runs without errors. I swear that the only difference between both versions is the use of a macro to call the qmlRegisterType template function.

    After the explanation of the problem, let me ask a pair of questions:

    • Is the QMLTYPE(T) macro correct or I made some kind of mistake typing that macro.
    • If the macro isn't the problem and it expands to the expected code, is the Qt runtime unable to register the type because the macro "doesn't look like a type registering"?

    I'm using Qt Creator (3.1.2) Based on Qt 5.3.1 (MSVC 2010, 32 bit)

    This isn'nt a blocking problem but I'd love to understand what is happening on the code, any guidance will be wellcomed.

    Thanks.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      shouldn't your macro not look like this?
      @
      #define QMLTYPE(T) qmlRegisterType<T>("CompanyName", 1, 0, "T");
      @

      also since you are using MSVC compiler you could compile witht he /P flag to see what the preprocessor expands to.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

        [quote author="raven-worx" date="1413891921"]shouldn't your macro not look like this?
        @
        #define QMLTYPE(T) qmlRegisterType<T>("CompanyName", 1, 0, "T");
        @[/quote]No, the macro is correct in the fourth parameter; the use of # stringifies the literal passed: "look here":https://gcc.gnu.org/onlinedocs/cpp/Stringification.html.[quote author="raven-worx" date="1413891921"]also since you are using MSVC compiler you could compile witht he /P flag to see what the preprocessor expands to.[/quote]On the other hand, I'm not compiling with MSVC, i'm compiling with mingw. When I click on Help > About Qt Creator, the information in the dialog that shows up is:

        bq. Qt Creator (3.1.2)
        Based on Qt 5.3.1 (MSVC 2010, 32 bit)

        I'm not sure why the MSVC is mentioned here, but I can assure that I'm using Qt Creator with mingw to compile.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          [quote author="PaperBirdMaster" date="1413892362"]No, the macro is correct in the fourth parameter; the use of # stringifies the literal passed: "look here":https://gcc.gnu.org/onlinedocs/cpp/Stringification.html.
          [/quote]

          right, my fault.

          [quote author="PaperBirdMaster" date="1413892362"]
          On the other hand, I'm not compiling with MSVC, i'm compiling with mingw. When I click on Help > About Qt Creator, the information in the dialog that shows up is:

          bq. Qt Creator (3.1.2)
          Based on Qt 5.3.1 (MSVC 2010, 32 bit)

          I'm not sure why the MSVC is mentioned here, but I can assure that I'm using Qt Creator with mingw to compile.[/quote]

          oh... thats the version QtCreator was built with.

          Nevertheless i would recommend that you compile the file with "gcc's -E flag":https://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Preprocessor-Options.html#Preprocessor-Options to see the resulting macro expansion.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            just tried (with msvc compiler though, since i don't have a mingw setup currently available):

            Your macro expands correctly to
            qmlRegisterType<MyType>("CompanyName", 1, 0, "MyType");

            You could try to check what the return values of the qmlRegisterType() calls are

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

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

              [quote author="raven-worx" date="1413900455"]You could try to check what the return values of the qmlRegisterType() calls are[/quote]

              My bad, I didn't think about that!

              I'll try when I got some free time.

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

                After weeks of giving up on the problem, I've found what went wrong.

                The macro is correct, and the code that uses the macro is correct too.

                The problem is the capitalization of "Component1" which is used on the qml files with the name "component1" (first letter lowercase).

                So the macro is publishing the type "Component1" while the code is using the type "component1"... it is a silly problem.

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

                  After weeks of giving up on the problem, I've found what went wrong.

                  The macro is correct, and the code that uses the macro is correct too.

                  The problem is the capitalization of "Component1" which is used on the qml files with the name "component1" (first letter lowercase).

                  So the macro is publishing the type "Component1" while the code is using the type "component1"... it is a silly problem.

                  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