Results 1 to 7 of 7

Thread: Connect signal to slot from different class

  1. #1
    Join Date
    Oct 2015
    Posts
    28
    Thanks
    10
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Connect signal to slot from different class

    Hi,

    I have a signal that I want to connect to a slot found in a different class. How do I do that?
    What I thought I could do:

    Class A header:
    Qt Code:
    1. #include "classB.h"
    2.  
    3. class ClassA
    4. {
    5.  
    6. Q_Object
    7.  
    8. public:
    9. ClassA();
    10. ...
    11. ...
    12.  
    13. signals:
    14. void stuffChange( const QString & );
    15.  
    16. private:
    17.  
    18. ClassB classb
    19. };
    To copy to clipboard, switch view to plain text mode 

    And in Class A constructor:
    Qt Code:
    1. connect(this, SIGNAL( stuffChange( const QString & ) ), &classb, SLOT( setStuff( const QString & ) ) );
    To copy to clipboard, switch view to plain text mode 

    Class B header:
    Qt Code:
    1. class ClassB
    2. {
    3.  
    4. Q_Object
    5.  
    6. public:
    7. ClassB();
    8. ...
    9. ...
    10.  
    11. public slots:
    12. void setStuff( const QString & );
    13.  
    14. private:
    15. QString stuff;
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 

    This gives me the error: LNK2019: unresolved external symbol "public: __ ...
    What is the right way to do this?
    Last edited by Leutzig; 10th December 2015 at 20:33.

  2. #2
    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: Connect signal to slot from different class

    The connect looks ok.
    Are both ClassA and ClassB derived from QObject?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    28
    Thanks
    10
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Connect signal to slot from different class

    Both classes look like this in the header (which was written by Qt when the project was made):

    Qt Code:
    1. classA : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. classB : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ...
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if that means that they are derived from Q_OBJECT.

  4. #4
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Connect signal to slot from different class

    It looks fine. If you added Q_OBJECT later then you might want to try running qmake again.

    May be showing a bit more of the actual code might be a better way to get more help..

  5. #5
    Join Date
    Oct 2015
    Posts
    28
    Thanks
    10
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Connect signal to slot from different class

    I've tried running qmake many times and deleting build directory, Clean All, made a new project and inserted every class and function individually, but when I add the object of the other class and use it in the connect, I get that error.
    The actual classes are these:

    ColorSelect header (ClassA):
    Qt Code:
    1. #ifndef COLORSELECT_H
    2. #define COLORSELECT_H
    3.  
    4. #include "device.h"
    5. #include "serialcom.h"
    6.  
    7. namespace Ui {
    8. class ColorSelect;
    9. }
    10.  
    11. class ColorSelect : public QWidget
    12. {
    13. Q_OBJECT
    14. //Q_PROPERTY(QColor color READ color NOTIFY colorChanged)
    15.  
    16. public:
    17. explicit ColorSelect(QWidget *parent = 0, SerialCom *cS = NULL);
    18. QWidget * getColorWheelWidgetPtr() const;
    19. QColor getRGBColor() const;
    20. void setItemData(QString, QString);
    21. ~ColorSelect();
    22.  
    23. signals:
    24. void colorChanged(QColor arg);
    25. void houseChange(const QString &);
    26. void unitChange(const QString &);
    27.  
    28. private slots:
    29. void updatePalette( const QColor & );
    30. void on_HouseAndUnitChanged( const QString & house, const QString & unit );
    31. void on_ColorChanged();
    32. void on_upButton_clicked();
    33. void on_downButton_clicked();
    34. void on_addDeviceButton_clicked();
    35. void on_deleteDeviceButton_clicked();
    36. void on_listWidget_itemDoubleClicked(QListWidgetItem *item);
    37. void on_chooseButton_clicked();
    38.  
    39. private:
    40. Ui::ColorSelect *ui;
    41. QColor rgbColor;
    42. SerialCom *serialCom;
    43. Device device;
    44. };
    45.  
    46. #endif // COLORSELECT_H
    To copy to clipboard, switch view to plain text mode 

    ColorSelect constructor from ColorSelect cpp:
    Qt Code:
    1. #include "colorselect.h"
    2. #include "ui_colorselect.h"
    3. #include "devicedialog.h"
    4. #include <qdebug.h>
    5.  
    6. ColorSelect::ColorSelect(QWidget *parent, SerialCom *serialCom) : QWidget(parent),
    7. ui(new Ui::ColorSelect)
    8. {
    9. ui->setupUi(this);
    10. connect(ui->redSlider, SIGNAL(sliderReleased()), SLOT(on_ColorChanged()));
    11. connect(ui->greenSlider, SIGNAL(sliderReleased()), SLOT(on_ColorChanged()));
    12. connect(ui->blueSlider, SIGNAL(sliderReleased()), SLOT(on_ColorChanged()));
    13. ui->redLine->setReadOnly(true);
    14. ui->greenLine->setReadOnly(true);
    15. ui->blueLine->setReadOnly(true);
    16. on_ColorChanged();
    17. this->serialCom = serialCom;
    18.  
    19. // highlights default device
    20. ui->listWidget->item(0)->setSelected(true);
    21. ui->listWidget->setCurrentItem(ui->listWidget->item(0));
    22.  
    23. // sets default values for default device
    24. ui->listWidget->item(0)->setData(Qt::UserRole, "A01");
    25.  
    26. connect( ui->colorWheelWidget, SIGNAL( colorChange( const QColor & ) ), this, SLOT( updatePalette( const QColor & ) ) );
    27. connect(this, SIGNAL( houseChange( const QString & ) ), &device, SLOT( setHouse( const QString & ) ) );
    28. connect(this, SIGNAL( unitChange( const QString & ) ), &device, SLOT( setUnit( const QString & ) ) );
    29. }
    To copy to clipboard, switch view to plain text mode 

    And Device header (class B):
    Qt Code:
    1. #ifndef DEVICE
    2. #define DEVICE
    3.  
    4. #include "serialcom.h"
    5.  
    6. class Device : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit Device(QWidget *parent = 0);
    12. QString getHouse();
    13. QString getUnit();
    14.  
    15. public slots:
    16. void setHouse(const QString &);
    17. void setUnit(const QString &);
    18.  
    19. protected:
    20.  
    21. private:
    22. QString house;
    23. QString unit;
    24.  
    25. private slots:
    26.  
    27. };
    28.  
    29. #endif // DEVICE
    To copy to clipboard, switch view to plain text mode 

    And Device cpp:
    Qt Code:
    1. #include "device.h"
    2.  
    3. QString Device::getHouse()
    4. {
    5. return house;
    6. }
    7.  
    8. QString Device::getUnit()
    9. {
    10. return unit;
    11. }
    12.  
    13. void Device::setHouse(const QString & house_)
    14. {
    15. house = house_;
    16. qDebug()<< "\nHouse value is:" << house;
    17. }
    18.  
    19. void Device::setUnit(const QString & unit_)
    20. {
    21. unit = unit_;
    22. qDebug()<< "Unit value is:" << unit;
    23. }
    To copy to clipboard, switch view to plain text mode 

    The full error is:
    colorselect.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Device::Device(class QWidget *)" (??0Device@@QEAA@PEAVQWidget@@@Z) referenced in function "public: __cdecl ColorSelect::ColorSelect(class QWidget *,class SerialCom *)" (??0ColorSelect@@QEAA@PEAVQWidget@@PEAVSerialCom@@ @Z)

    And another related error I get:
    debug\colorselect.exe:-1: error: LNK1120: 1 unresolved externals

    I can also attach the whole project if you want.

  6. #6
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Connect signal to slot from different class

    Where is the Device(QWidget* parent) constructor definition? I can't see it in Device.cpp..

  7. The following user says thank you to Vikram.Saralaya for this useful post:

    Leutzig (10th December 2015)

  8. #7
    Join Date
    Oct 2015
    Posts
    28
    Thanks
    10
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Connect signal to slot from different class

    Yep, that was it. There wasn't supposed to be a constructor and I completely overlooked the one Qt made in the header. Thanks Vik!

Similar Threads

  1. Connect Signal/Slot to any instance of another class
    By squeegedog in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2014, 08:43
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. Replies: 9
    Last Post: 30th June 2010, 19:36
  4. Replies: 12
    Last Post: 18th September 2008, 15:04
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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.