Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Linking error with slots containing non-qt types

    General and Desktop
    2
    3
    1013
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      cranky last edited by

      I'm using Qt 5.2.1 on Visual Studio 2010 with Qt Vs Add-in 1.2.3-alpha. Whenever I try to compile a class which have slots/signals referring to non-qt types, I get linking error. I setup my project like this:

      mydata.h
      @#pragma once
      #include <QtCore/QMetaType>
      struct MyData
      {
      int a;
      int b;
      };

      Q_DECLARE_METATYPE(MyData);@

      testslot.h
      @#pragma once
      #include <QtCore/QObject>
      class MyData;
      class TestSlot : public QObject
      {
      Q_OBJECT
      public:
      TestSlot(void);
      ~TestSlot(void);

      public slots:
      void aSlot(const MyData& data);
      };@

      testslot.cpp
      @#include "testslot.h"
      #include <QtCore/QMetaType>
      #include "mydata.h"
      TestSlot::TestSlot(void)
      {
      qRegisterMetaType<MyData>("MyData");
      }

      TestSlot::~TestSlot(void)
      {
      }

      void TestSlot::aSlot( const MyData& data )
      {
      data.a;
      }@

      main.cpp
      @
      #include <QtCore/QCoreApplication>
      #include <QtCore/QMetaType>
      #include "testslot.h"

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);
      TestSlot t;
      return a.exec();
      }@

      Error from the linker:
      @Error 1 error LNK2019: unresolved external symbol "public: void __thiscall TestSlot::aSlot(class MyData const &)" (?aSlot@TestSlot@@QAEXABVMyData@@@Z) referenced in function "private: static void __cdecl TestSlot::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestSlot@@CAXPAVQObject@@W4Call@QMetaObject@@HPAPAX@Z) D:\Visual Studio\Projects\Qt\Test\Test\moc_testslot.obj@

      1 Reply Last reply Reply Quote 0
      • M
        mcosta last edited by

        Hi,

        your code works for me (MinGW)!!.
        The only difference is that I not used #pragma once directive but the standard guards

        @
        #ifndef MY_FILE_H
        #define MY_FILE_H

        ....

        #endif // MY_FILE_H
        @

        Anyway i think it's better to use
        @qRegisterMetaType<MyData>("MyData");@

        outside TestSlot implementation (for example in main)

        @
        #include <QCoreApplication>

        #include "TestSlot.h"
        #include "MyData.h"

        int main(int argc, char *argv[])
        {
        QCoreApplication a(argc, argv);

        qRegisterMetaType<MyData>("MyData");
        
        TestSlot t;
        
        return a.exec(&#41;;
        

        }
        @

        UPDATE: reading from Qt Doc "here":http://qt-project.org/doc/qt-5/qmetatype.html#qRegisterMetaType you don't need to use qRegisterMetaType

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply Reply Quote 0
        • C
          cranky last edited by

          -This problem does not occur with QtCreator nor VS Add-in 1.2.2. There must be a bug in version 1.2.3-alpha.-
          Edited: I made a mistake when forward declaring: MyData is defined as a struct, not a class. VS compiler made a clear distinction between these two keywords, that's why the linking error shows up.
          Thanks for your answer.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post