Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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)

    Q_DECLARE_METATYPE(CCS::DataManagerState)
    
    using namespace ::testing;
    class TestOpcUa : public Test
    {
    public:
        TestOpcUa()
        {
            //.....
        }
    
        ~TestOpcUa()
        {
            delete _selectedConfValid;
            delete _opcUaObjectValid;
        }
    
    protected:
        MockOpcConf* _selectedConfValid = nullptr;
        OpcUa* _opcUaObjectValid = nullptr;
    };
    
    TEST_F(TestOpcUa, checkHandshake)
    {
        QSignalSpy readySpy(_opcUaObjectValid, SIGNAL(newManagerState(CCS::DataManagerState)));
        _opcUaObjectValid->connect();
        readySpy.wait();
        ASSERT_EQ(readySpy.count(), 1);
        ASSERT_EQ(readySpy.at(0).at(0).value<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
    {
        Q_NAMESPACE
    
        enum class DataManagerState
        {None, Configure, Ready, Active, Waiting, Stop};
        Q_ENUM_NS(DataManagerState)
    }
    

  • Qt Champions 2019

    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