Results 1 to 3 of 3

Thread: qtimer and signals

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default qtimer and signals

    I worked through a youtube tutorial on signals and slots, and I thought I had a pretty good understanding of it. I tried playing with the code a little bit to test some things, and I thought this should work, but I'm not sure exactly where it goes wrong.

    The original program created 2 person objects and had person A tell person B something, and person B then repeated it. What I tried to do was connect a timer to person A to make that person tell person B the same thing every second, but when I run it, the time doesn't make them do anything. I'm not sure where I am going wrong.

    I created a test slot just to be sure the timer was ticking, and it is, so I know my problem lies somewhere in the timer connection to the person A slot. Any help or tips would be much appreciated, Thanks!

    Here is the code:

    Qt Code:
    1. #ifndef PERSON_H
    2. #define PERSON_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QDebug>
    7.  
    8. class Person : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit Person(QObject *parent = 0);
    13.  
    14. QString Name;
    15.  
    16.  
    17.  
    18. signals:
    19. void Speak(QString words);
    20.  
    21. public slots:
    22. void Listen(QString words);
    23. void Gossip();
    24.  
    25. };
    26.  
    27. #endif // PERSON_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef WATERCOOLER_H
    2. #define WATERCOOLER_H
    3.  
    4. #include <QObject>
    5.  
    6. class WaterCooler : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit WaterCooler(QObject *parent = 0);
    11.  
    12. signals:
    13.  
    14. public slots:
    15. void testTimer();
    16.  
    17. };
    18.  
    19. #endif // WATERCOOLER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "person.h"
    2. #include <QTimer>
    3.  
    4. Person::Person(QObject *parent) :
    5. QObject(parent)
    6. {
    7.  
    8. }
    9.  
    10. void Person::Listen(QString Words)
    11. {
    12. qDebug() << Name << " says someone told me... " << Words;
    13. }
    14.  
    15. void Person::Gossip()
    16. {
    17. QString Words = " says I heard so and so did something.";
    18. qDebug() << Name << Words ;
    19. emit Speak(Words);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "watercooler.h"
    2. #include "person.h"
    3. #include <QTimer>
    4.  
    5. WaterCooler::WaterCooler(QObject *parent) :
    6. QObject(parent)
    7. {
    8. Person Cathy;
    9. Person Bob;
    10.  
    11. Cathy.Name = "Cathy";
    12. Bob.Name = "Bob";
    13.  
    14. connect(&Cathy, SIGNAL(Speak(QString)), &Bob, SLOT(Listen(QString)));
    15.  
    16. QTimer *timer = new QTimer(this);
    17. connect ( timer, SIGNAL(timeout()), &Cathy,SLOT(Gossip()));
    18. connect ( timer, SIGNAL(timeout()), this, SLOT(testTimer()));
    19. timer->start(1000);
    20.  
    21. Cathy.Gossip();
    22. }
    23.  
    24. void WaterCooler::testTimer()
    25. {
    26. qDebug() << "Timer working." << endl;
    27. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: qtimer and signals

    Look at WaterCooler constructors.
    you have this code:
    Qt Code:
    1. Person Cathy;
    2. Person Bob;
    To copy to clipboard, switch view to plain text mode 
    It's mean when WaterCooler instance constructed, Cathy and Bob destroyed because both are not pointer.
    It's safe to use pointer and set the parent to WaterCooler instance so both of them destroyed when WaterCooler instance destroyed.
    Qt Code:
    1. Person* Cathy = new Person(this);
    2. Person* Bob = new Person(this);
    To copy to clipboard, switch view to plain text mode 
    ~ We are nothing in this universe ~

  3. #3

    Default Re: qtimer and signals

    Ah, that was the problem. I guess I forgot I was writing the code in the constructor. Thanks for the help.

Similar Threads

  1. Replies: 6
    Last Post: 29th April 2011, 15:22
  2. Replies: 1
    Last Post: 24th October 2010, 11:09
  3. QTimer
    By link7 in forum Newbie
    Replies: 4
    Last Post: 2nd June 2009, 08:35
  4. conflict between boost signals and Qt signals
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 09:46
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18

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.