QmlRegisterType template fails if used in macro



  • 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.


  • Moderators

    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.



  • [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.


  • Moderators

    [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.


  • Moderators

    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



  • [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.



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.