Results 1 to 4 of 4

Thread: connecting signal from thread to slot in a cpp file

  1. #1
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default connecting signal from thread to slot in a cpp file

    I am trying to connect a signal from a thread to a slot in a cpp file.
    I am able to successfully connect it to Main Window slot,but not through a cpp file.
    Here is what I am doing
    //abc.h
    Qt Code:
    1. #ifndef ABC_H
    2. #define ABC_H
    3. #include <QObject>
    4.  
    5. class abc : public QObject
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit abc(QObject *parent = 0);
    10. public slots:
    11. void AbcInvokeSlot();
    12. };
    13.  
    14. #endif // ABC_H
    To copy to clipboard, switch view to plain text mode 
    //abc.cpp
    Qt Code:
    1. #include "abc.h"
    2. abc::abc(QObject *parent) :
    3. QObject(parent)
    4. {
    5. }
    6. void abc :: AbcInvokeSlot()
    7. {
    8. //some code
    9. }
    To copy to clipboard, switch view to plain text mode 

    //main.cpp
    Qt Code:
    1. QApplication app(argc, argv);
    2. MainWindow win;
    3. abc a;
    4. win.show();
    5. SThread sthread;
    6. QObject::connect( & sthread, SIGNAL( SInSignal() ),& win, SLOT( SOutSlot() ) ); //correctly working
    7. QObject::connect( & sthread, SIGNAL( SInSignal() ),& a, SLOT( AbcInvokeSlot() ) ); //not working
    8. sthread.start();
    9. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    I am getting the error :
    undefined reference to 'vtable for abc'
    I tried changing
    Qt Code:
    1. abc a;
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. abc a=new abc;
    To copy to clipboard, switch view to plain text mode 

    then I am getting this error:
    no matching function for call to 'QObject::connect(SThread*, const char*, abc**, const char*)'
    May I know what I am doing wrong and what is the correct procedure??
    Last edited by prkhr4u; 17th February 2014 at 06:12. Reason: updated contents

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: connecting signal from thread to slot in a cpp file

    Rerun qmake and rebuild your program.

  3. The following user says thank you to ChrisW67 for this useful post:

    prkhr4u (17th February 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: connecting signal from thread to slot in a cpp file

    As ChrisW67 said, run qmake again.

    Your code is fine, the error is caused by adding Q_OBJECT to a header after the header has been added to the project file and the related qmake run.

    Classes with Q_OBJECT need to be processed by MOC and these processing instructions are created by qmake.
    If you add Q_OBJECT after a header file has been "seen" by qmake, then certain bit will be missing at link time.

    Rerunning qmake (and completing the hew build) solves that.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    prkhr4u (17th February 2014)

  6. #4
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: connecting signal from thread to slot in a cpp file

    Thank you for the solution and explanation.It exactly solved my problem!!

Similar Threads

  1. QSslSocket: connecting SIGNAL and SLOT
    By Mobility in forum Qt Programming
    Replies: 14
    Last Post: 1st January 2013, 18:59
  2. Connecting custom signal and slot
    By DmitryNik in forum Newbie
    Replies: 4
    Last Post: 12th September 2011, 15:15
  3. Connecting signal and slot by name
    By grayfox in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 10:00
  4. Replies: 3
    Last Post: 17th November 2010, 15:12
  5. Connecting signal to custom slot?
    By dbrmik in forum Qt Tools
    Replies: 2
    Last Post: 30th April 2009, 10:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.