Slot executions problems in a new thread



  • Hi all,
    I got a strange problem. You can see the following code.

    I have an object as this:
    @
    GestoreComunicazioneBase::GestoreComunicazioneBase() :
    QObject(0)
    {
    ...
    ...
    m_thread = new QThread;
    this->moveToThread(m_thread);
    m_thread->start();
    ...
    ...
    }
    @

    and another object that Inherit GestoreComunicazioneBase :

    @
    GestoreComunicazioneEmh::GestoreComunicazioneEmh() :
    {
    QTimer::singleShot(10000, this, SLOT(leggiElencoObis()));
    }
    @

    where leggiElencoObis() is a slot of GestoreComunicazioneEmh.

    When executing I get the following error message:
    @
    Object::connect: No such slot GestoreComunicazioneBase::leggiElencoObis() in ...
    @

    so it seems it's looking for GestoreComunicazioneBase::leggiElencoObis() while the slot is GestoreComunicazioneEmh::leggiElencoObis() .


  • Moderators

    Do you have a "Q_OBJECT":http://developer.qt.nokia.com/doc/qt-4.8/qobject.html#Q_OBJECT macro also in GestoreComunicazioneEmh class definition?


  • Moderators

    Are you missing the Q_OBJECT macro?



  • Now I have:
    @
    class GestoreComunicazioneEmh : public GestoreComunicazioneBase
    {
    Q_OBJECT
    public:
    GestoreComunicazioneEmh()
    ...
    ...
    @

    But with Q_OBJECT the compilation fails:
    @
    Desktop_Qt_4_7_4_for_GCC__Qt_SDK__Debug/../satellite/gestorecomunicazioneemh.cpp:10: undefined reference to `vtable for GestoreComunicazioneEmh'
    @

    I think it's should be a very stupid problem but I can't find it...



  • Re-run qmake. If your class is defined within a <code>.cpp</code> file make sure you <code>#include "filename.moc"</code>.



  • Ok, the problem was the missing Q_OBJECT but to solve compilation problem I had to remove all build directory...



  • It seems that "Rebuild project" in QtCreator was not enough...

    Thanks all!



  • So is it solved? Because it seems that you haven't added the QObject as base class of GestoreComunicazioneEmh:

    @
    class GestoreComunicazioneEmh : public QObject, public GestoreComunicazioneBase
    {
    Q_OBJECT
    public:
    GestoreComunicazioneEmh(QObject* parent=NULL) : QObject(parent) {}
    };
    @



  • Yes I solved by adding Q_OBJECT (a very stupid question :-) ) but when adding it the project building failed also after "Rebuilding it".
    To solve the problem I also had to remove all files (Makefile too ) in build directory.



  • QObject is in GestoreComunicazioneBase:
    @
    class GestoreComunicazioneBase : public QObject
    {
    Q_OBJECT
    public:
    ...
    ...
    @



  • [quote author="favoritas37" date="1327329544"]So is it solved? Because it seems that you haven't added the QObject as base class of GestoreComunicazioneEmh:

    @
    class GestoreComunicazioneEmh : public QObject, public GestoreComunicazioneBase
    {
    Q_OBJECT
    public:
    GestoreComunicazioneEmh(QObject* parent=NULL) : QObject(parent) {}
    };
    @[/quote]

    This is WRONG! GestoreComunicazioneBase already inherits from QObject, so GestoreComunicazioneEmh is also a QObject subclass, albeit not a direct child class. Adding QObject to the inheritance list makes GestoreComunicazioneEmh in fact inherit from QObject twice, which is a bad, bad, bad idea.



  • Sorry Volker, i mentioned it because i haven't noticed that the base class already inherits from QObject. I wouldn't suggest to inherit twice from QObject.



  • [quote author="favoritas37" date="1327332502"]Sorry Volker, i mentioned it because i haven't noticed that the base class already inherits from QObject. I wouldn't suggest to inherit twice from QObject. [/quote]

    No problem, everyone overlooks something now and then :) I just wanted to make sure that another potential trouble maker does sneak in :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.