November 10, 2010

phamtv phamtv
Lab Rat
135 posts

slot in inherited class is not working…

 

I have an inherited class from a base class that is derived from QObject. Can someone tell me why my inherited class slot is not being invoked..

  1. class cls_A : public QObject
  2. {
  3.     Q_OBJECT
  4.  
  5. private:
  6.      explicit cls_A(QObject *parent = 0);
  7.  
  8.     // etc......
  9. }
  10.  
  11.  
  12. class cls_B : public cls_A
  13. {
  14. private:
  15.      cls_B()
  16.      {
  17.            // instantiate Serial Device Enumerator and get available serial ports
  18.           m_sde = new SerialDeviceEnumerator(this);
  19.           connect(m_sde, SIGNAL(hasChanged(QStringList)), this, SLOT(GetDevices(QStringList)));
  20.           m_sde->setEnabled(true);
  21.      }
  22.  
  23. private slots:
  24.     void GetDevices(const QStringList &list)
  25.     {
  26.            // do something....
  27.     }
  28.    //......
  29. }

I run the application but GetDevices is not being invoked… Does cls_B have to have the Q_OBJECT declaration?

5 replies

November 10, 2010

Volker Volker
Robot Herder
5428 posts

Yes, every class that inherits from QObject – regardless wheter direct or in an inheritance path – must include the Q_OBJECT macro in the private section of the class definition.

November 10, 2010

phamtv phamtv
Lab Rat
135 posts

when I do place the Q_OBJECT macro, I get an error indicating ¨error: undefined reference to `vtable for cls_B¨… what does this error imply?

November 10, 2010

Volker Volker
Robot Herder
5428 posts

Did you re-run qmake? It must pick up the new header and include some additional build steps for a QObject class with the Q_OBJECT macro in it. You also must add the header file to the HEADERS variable in the .pro file.

November 11, 2010

anselmolsm anselmolsm
Ant Farmer
417 posts

Also, if you declare and implement classes in the same file filename, you’ll need a

  1. #include "filename.moc"

 Signature 

Anselmo L. S. Melo (anselmolsm)
www.anselmolsm.org

November 11, 2010

Polto Polto
Lab Rat
21 posts

hi i think i works for me but
try to put the implement of the class constructor and other function in a cpp file not within the class
and put Q_OBJECT for the two classes

 Signature 

Polto

 
  ‹‹ External GPS on windows      Serial port number in windows ››

You must log in to post a reply. Not a member yet? Register here!