Results 1 to 12 of 12

Thread: emit signal and recognizing in a different class

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default emit signal and recognizing in a different class

    Hi,
    I am emitting a signal from A class and anticipating that the connect statemt in the B class connects my signal from A class to the slot function in the B class..
    Two ways i understood for this !..
    1, If i put an #include A statement in the B,
    and/or
    2, If B inherits from A,
    Please ,if any one can point out which is the right approach or if both are dumb :P and i shud do something else..
    Thanks already ...

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: emit signal and recognizing in a different class

    You don't need to derive or create the objects one into the other, you can also create the objects in the same scope, like:
    Qt Code:
    1. int main()
    2. {
    3. //...
    4. A objA;
    5. B objB;
    6. QObject::connect(&objA, SIGNAL(boo()), &objB, SLOT(onBooDoSomething()));
    7. //...
    8. }
    To copy to clipboard, switch view to plain text mode 
    Basically you just need to pointers to those objects (and of course you need to make sure that the object "live" in the same time )
    Read more here

  3. #3
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit signal and recognizing in a different class

    it seems that my problem statement wasnt accurate to be properly understood.... i will repeat my question again
    i have two classes for eg A and B ..
    Qt Code:
    1. #include "B"
    2. class A: public QObject
    3. {
    4. ..
    5. B bObj;
    6. ..
    7. connect (&bObj,SIGNAL(bSignal()), this, SLOT(anyAFunc()));
    8. ..
    9. }
    10. ------------------------------------------------------------------------
    11. class b: public QWIdget
    12. {
    13. ..
    14. emit bSignal();
    15. ..
    16. ..
    17. }
    To copy to clipboard, switch view to plain text mode 
    Now this techque seems to be a flaw as the compiler complains
    Object::connect: No such signal b::bSignal()

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: emit signal and recognizing in a different class

    Didn't you forgot to declare bSignal with signals: access specifier?

    //and... as i said in previous post you don't have to use composition (or inheritance) for signal-slot connection to work.

  5. #5
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit signal and recognizing in a different class

    the bSignal is declared with the signals access specifier....

  6. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit signal and recognizing in a different class

    please show the header with the definition of B

  7. #7
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: emit signal and recognizing in a different class

    Qt Code:
    1. #include <QWidget>
    2.  
    3. class b: public QWidget
    4. {
    5.  
    6. Q_OBJECT
    7.  
    8. public:
    9.  
    10. // Public functions here
    11.  
    12. public slots:
    13.  
    14. // Public slots here
    15.  
    16. signals:
    17.  
    18. void bSignal(); // <-- This is how you declare a signal
    19.  
    20. protected:
    21.  
    22. // Protected members here
    23.  
    24. private:
    25.  
    26. // Private members here
    27. };
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit signal and recognizing in a different class

    B.hpp
    Qt Code:
    1. #include <QWidget>
    2. #include <QGridLayout>
    3. #include <QLabel>
    4. #include <QToolButton>
    5. #include <QIcon>
    6. #include <QComboBox>
    7.  
    8. namespace UpstreamPrivateNamespace{
    9.  
    10. class B : public QWidget
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. B( QWidget * ParentP = 0 );
    16.  
    17. signals:
    18. .....
    19. void bSIgnal(const QString&);
    20.  
    21. public slots:
    22.  
    23. void bSlot(const QString& );
    24.  
    25.  
    26. private:
    27. QIcon * iconVar;
    28. QComboBox * boxVar;
    29. };
    30. }
    To copy to clipboard, switch view to plain text mode 


    B.cpp

    Qt Code:
    1. b::b( QWidget * ParentP )
    2. : QWidget( ParentP )
    3. {
    4. ..
    5. this->boxVar = new QComboBox(this);
    6. connect(this->boxVar, SIGNAL(currentIndexChanged(const QString& )), this, SLOT(bSlot(const QString& )));
    7. ..
    8. }
    9. void b::bSlot(const QString& var)
    10. {
    11.  
    12. emit bSignal(var);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit signal and recognizing in a different class

    does bSlot get called when you change the combobox index?

    btw, you can connect signals with signals directly:

    Qt Code:
    1. connect(this->boxVar, SIGNAL(currentIndexChanged(const QString& )), this, SIGNAL(bSignal(const QString& )));
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit signal and recognizing in a different class

    does bSlot get called when you change the combobox index?
    yes

    btw, you can connect signals with signals directly:
    i remember this now, this is what happens when u do programming after 3 years gap ..

    I will also mention the A class ..

    A.hpp
    Qt Code:
    1. ..
    2. public slots:
    3. void setDev(const QString &);
    4. private:
    5. B * bObj ;
    6. ..
    To copy to clipboard, switch view to plain text mode 

    A.cpp

    Qt Code:
    1. constructor
    2. bObj = new b();
    3. connect(bObj, SIGNAL(bSignal(const QString &)), this, SLOT(setDev(const QString &)));
    4. // constructor ends here
    5.  
    6. void setDev(const QString & dev)
    7. {
    8. QLOG_INFO() << dev ;
    9. }
    To copy to clipboard, switch view to plain text mode 

    The const was missing from the connect statemet in the Signal ... the 'No such signal ' warning disapears but the connect statement doesnt work ...as the A:setDev function isnt called when the combobox value changes ....
    Last edited by salmanmanekia; 18th October 2011 at 08:26.

  11. #11
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: emit signal and recognizing in a different class

    Check your spelling.

    void bSIgnal(const QString&);
    !=
    void bSignal(const QString&);

  12. #12
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: emit signal and recognizing in a different class

    Thanks boudie its a typo in the forum .....in the code the spellings are correct !

Similar Threads

  1. [PyQt] send and emit an customer class....
    By cascoin in forum Qt Programming
    Replies: 5
    Last Post: 25th July 2013, 18:25
  2. Replies: 2
    Last Post: 3rd May 2011, 20:22
  3. emit signal from script
    By wookoon in forum Newbie
    Replies: 5
    Last Post: 6th July 2010, 14:37
  4. signal doesnt emit
    By mark2804 in forum Newbie
    Replies: 2
    Last Post: 25th December 2008, 22:36
  5. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

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
  •  
Qt is a trademark of The Qt Company.