Results 1 to 10 of 10

Thread: Handling signal-slot

  1. #1
    Join Date
    Oct 2009
    Posts
    105
    Thanked 4 Times in 2 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Handling signal-slot

    Hi All
    I've a doubt on signal and slot mechanisim. E.g

    Qt Code:
    1. class A
    2. {
    3. signals:
    4. void sig();
    5. }
    6.  
    7. class B
    8. {
    9. public:
    10. A objOfA;
    11. }
    12.  
    13.  
    14. class C
    15. {
    16. B objOfB;
    17. C();
    18. public slots:
    19. void slot();
    20. }
    21.  
    22. C::C()
    23. {
    24. connect(&objOfB.objOfA,SIGNAL(sig(),this SLOT(slot()); /// Can we handle like this???
    25. }
    To copy to clipboard, switch view to plain text mode 


    My doubt is can we handle the signal as above?

    Thank you all.
    Last edited by sudhansu; 30th January 2010 at 06:53.

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Handling signal-slot

    If you add missing parentheses to connect() statement and make your classes derived from QObject then this should work.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Oct 2009
    Posts
    105
    Thanked 4 Times in 2 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Handling signal-slot

    Quote Originally Posted by faldżip View Post
    If you add missing parentheses to connect() statement and make your classes derived from QObject then this should work.
    Hi,
    By mistake i forgot to add paranthesis,. And my class is derived from QDialog,(and QDialog is derived fromQWidget->QObject), SO i think no need to directly derived it from QObject.
    But still its not working?.

  4. #4
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Handling signal-slot

    Eh, what exactly isn't working?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Handling signal-slot

    Can you explain what does it mean "not working"? Any warnings on console output? Can you show exact code? Or prepare minimal compilable example reproducing your problem (but do not give us your whole project, but write something like in first post but make compilable).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    Oct 2009
    Posts
    105
    Thanked 4 Times in 2 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Handling signal-slot

    Quote Originally Posted by faldżip View Post
    Can you explain what does it mean "not working"? Any warnings on console output? Can you show exact code? Or prepare minimal compilable example reproducing your problem (but do not give us your whole project, but write something like in first post but make compilable).
    Below i'm posting the code


    Qt Code:
    1. class CMainWindow : public QDialog, public Ui::Dialog
    2. {
    3. Q_OBJECT
    4.  
    5. MessageBox MessageBoxObj;
    6.  
    7. public:
    8. CMainWindow();
    9.  
    10.  
    11. public slots:
    12. void HandelMessage(QString i_strMessage);
    13. }
    14. CMainWindow()::CMainWindow()
    15. {
    16. connect(&(MessageBoxObj.networklayerObj),SIGNAL(MessageFromNetworkLayer(QSTring )),this,SLOT( HandelMessage(QString)));
    17. }
    18.  
    19. class MessageBox : public QDialog, public Ui::MessageDialog
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. Network networklayerObj;
    25. }
    26.  
    27. class Network:public QObject
    28. {
    29. O_OBJECT
    30.  
    31. signals:
    32. void MessageFromNetworkLayer(QSTring message);
    33. }
    To copy to clipboard, switch view to plain text mode 

    Please ignore the spelling mistakes.

  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Handling signal-slot

    Ok, so:
    1. What is not working?
    2. Can check if there is any warning on console output?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #8
    Join Date
    Oct 2009
    Posts
    105
    Thanked 4 Times in 2 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Handling signal-slot

    Quote Originally Posted by faldżip View Post
    Ok, so:
    1. What is not working?
    2. Can check if there is any warning on console output?
    While debugging, i found that signal is emiting with some data, but its not connected to slot.
    Debug statment inside slot also not priting, if i run the application.

  9. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Handling signal-slot

    Don't you have any warning on console output like:
    WARNING: no matching slot ...
    ?

    For me this is working:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. class A : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. A() : QObject(0), eventno(0) {
    8. startTimer(2000);
    9. }
    10. protected:
    11. void timerEvent(QTimerEvent *) {
    12. if (++eventno == 2)
    13. else
    14. emit message("Some useful message!");
    15. }
    16. private:
    17. int eventno;
    18. signals:
    19. void message(const QString &str);
    20. };
    21.  
    22. class B : public QObject
    23. {
    24. Q_OBJECT
    25. public:
    26. B() : QObject(0) { }
    27. A aObj;
    28. };
    29.  
    30. class C : public QObject
    31. {
    32. Q_OBJECT
    33. public:
    34. C() : QObject(0) {
    35. connect(&(bObj.aObj), SIGNAL(message(QString)), this, SLOT(showMessage(QString)));
    36. }
    37. public slots:
    38. void showMessage(const QString &str) {
    39. qDebug(str.toLocal8Bit().constData());
    40. }
    41. private:
    42. B bObj;
    43. };
    44.  
    45. int main(int argc, char **argv)
    46. {
    47. QCoreApplication a(argc, argv);
    48. C cObj;
    49. return a.exec();
    50. }
    51.  
    52. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  10. #10
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Handling signal-slot

    I 'm not sure whether you typed your code or just copied-pasted, but your class definition has to end with "};".
    I'm a rebel in the S.D.G.

Similar Threads

  1. Signal and slot
    By jayreddy in forum Qt Programming
    Replies: 12
    Last Post: 15th December 2009, 05:05
  2. SIGNAL and SLOT
    By sophister in forum Qt Programming
    Replies: 5
    Last Post: 25th April 2009, 08:03
  3. Signal & Slot
    By LordQt in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2007, 22:22
  4. Replies: 1
    Last Post: 8th November 2007, 17:11
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.