Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Linking error with slots containing non-qt types
QtWS25 Last Chance

Linking error with slots containing non-qt types

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k Views
  • 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 Offline
    C Offline
    cranky
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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
      0
      • C Offline
        C Offline
        cranky
        wrote on last edited by
        #3

        -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
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved