Search code examples
c++qtqtestlib

"no member named 'qt_metatype_id'" for QFETCH


I added this line to my QTestLib test class:

QFETCH(MyType, value);

When trying to compile the test class, I get this error:

error: no member named 'qt_metatype_id' in 'QMetaTypeId<MyType>'

Solution

  • When using QTest::addColumn and QFETCH for a type isn't built into Qt, you have to add this line:

    Q_DECLARE_METATYPE(MyType);
    

    It goes above the start of your test class. For example:

    Q_DECLARE_METATYPE(MyType);
    
    class MyTest : public QObject
    {
        Q_OBJECT
    
    private slots:
    
        void testSomething_data()
        {
            QTest::addColumn< MyType >("expectedValue");
            ...
        }
        void testSomething()
        {
            QFETCH(MyType, expectedValue);
            ...
    }
    };