Nominate our 2022 Qt Champions!

qRegisterMetaType of custom C-style struct for usae with QSignalSpy in QTest

  • Hi,
    1. I have C headers for driver, which contains a lot of structures. I use the structures as signals' arguments. Here is example of a such a like structure (most simple one just for example)

    typedef struct
    	uint16_t Capabilities;
    } PingSrspFormat_t;

    2. I've made simplest QTest, and in 'testCase1()' try to get signals from my DUT object this way:

    	// 1. Register custom C-Style struct type
    	// 2. Connect signal of my DUT object (znpHost) already instantiated above
    QSignalSpy spy(znpHost,SIGNAL(pfnSysPingSrsp(const PingSrspFormat_t&)));
    	// 3. just in case clear spy list
    	// 4. issue method of my DUT object to get signal from it
    	// 5. wait for a while..
    	// 6. get the signal argument
    PingSrspFormat_t result = qvariant_cast<PingSrspFormat_t>(;
    	// 7. check the argument
    QVERIFY(result.Capabilities == 0x0000); 

    3. due compilation get an error:

    /home/dev/Qt/5.5/gcc_64/include/QtCore/qglobal.h:703: error: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure<false>'
         enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) = sizeof(QStaticAssertFailure<!!(Condition)>)}

    Any suggestions how to fix the compilation error are very welcome


  • Moderators

    Do you have Q_DECLARE_METATYPE(PingSrspFormat_t) at the appropriate place?

    Kind regards.

  • Hi!

    • Well, should i put the macros in C headers?
    • Am I correct if say that for signal/slot usage of qRegisterMetaType is enough?

    Thanks for reply

  • Moderators

    Usually you put the macro after the declarations, however I believe the moc will be forgiving enough if you put it into your own header (not to modify the externals). Something like this in mycwrapheader.h:

    #include <somecheader.h>
    // ... more code

    Am I correct if say that for signal/slot usage of qRegisterMetaType is enough?

    Whether or not this is true is besides the point, because you're making a qvariant_cast and for that you'd need the Q_DECLARE_METATYPE macro. Additionally, you need to register the type with the runtime (through qRegisterMetaType) only if you're using queued connections (usually when multithreading is involved).

    Kind regards.

  • @kshegunov wow!
    Seems you are right I've just put the macros at hte top of my Unity test *.cpp right after all #includes and the compilation passed!
    Thanks a lot for the advice!

  • Moderators

    No problem, however you should really put that macro in a header .h, not in your source file .cpp. :)

  • @kshegunov yes, you are correct, but for fast issue fix it was ok to put it in *.cpp :) now I can gloss the code with calm after your great advice!

Log in to reply