Important: Please read the Qt Code of Conduct -

Compare QVariable with Enum of DLL

  • I try to compare a QVariable of a QSignalSpy with a Enum in a test function. I found Q_ENUM_NS, but I got a linker error. I have rebuilt it and run qmake.

    Error Message: Undefined reference to CCS::staticMetaObject

    testopcua.cpp (most parts)

    using namespace ::testing;
    class TestOpcUa : public Test
            delete _selectedConfValid;
            delete _opcUaObjectValid;
        MockOpcConf* _selectedConfValid = nullptr;
        OpcUa* _opcUaObjectValid = nullptr;
    TEST_F(TestOpcUa, checkHandshake)
        QSignalSpy readySpy(_opcUaObjectValid, SIGNAL(newManagerState(CCS::DataManagerState)));
        ASSERT_EQ(readySpy.count(), 1);
        ASSERT_EQ(<CCS::DataManagerState>(), CCS::DataManagerState::Ready); //Linker error
    int main(int argc, char *argv[])
        QCoreApplication app(argc, argv);
        testing::InitGoogleTest(&argc, argv);
        testing::InitGoogleMock(&argc, argv);
        return RUN_ALL_TESTS();

    enums.h of DLL

    #include <QObject>
    namespace CCS
        enum class DataManagerState
        {None, Configure, Ready, Active, Waiting, Stop};

  • Lifetime Qt Champion

    since it's an enum simply use QVariant::tonInt() and do a static cast to the enum

  • @Christian-Ehrlicher thanks for your answer.

    If found a second solution here

    But to begin I had to add: qRegisterMetaTypeCCS::DataManagerState("state");

    Now it works.

Log in to reply