Results 1 to 4 of 4

Thread: Signal is emtted, slot does not fire.

  1. #1
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Signal is emtted, slot does not fire.

    Having a bit of an issue with my Slots/Signals. I have distilled the code down to basics and am using it in a scratch project to help figure it out. I have run this in debug mode and placed a breakpoint in the moc file where the signal runs. Sure enough, it hits. However, the initStuff method is only being called once.

    manager.h
    Qt Code:
    1. #ifndef MANAGER_H
    2. #define MANAGER_H
    3.  
    4. #include <QObject>
    5.  
    6. class Manager : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. static Manager* instance();
    11. static void init();
    12. void start();
    13. bool hasStuff() {return mStuff.size() > 0;}
    14. private:
    15. explicit Manager(QObject *parent = 0);
    16. static Manager* _instance;
    17.  
    18. QList<Channel*> mStuff;
    19. void processStuff();
    20. void loadStuffQueue();
    21.  
    22. signals:
    23. void emptyQueue();
    24.  
    25. public slots:
    26. void initStuff();
    27. };
    28.  
    29. #endif // MANAGER_H
    To copy to clipboard, switch view to plain text mode 

    manager.cpp
    Qt Code:
    1. #include "manager.h"
    2.  
    3. Manager::Manager(QObject *parent) :
    4. QObject(parent)
    5. {
    6. mThread = new QThread(0);
    7. moveToThread(mThread);
    8. }
    9.  
    10. void Manager::init()
    11. {
    12. _instance = new Manager();
    13. connect(_instance, SIGNAL(emptyQueue()), _instance, SLOT(initStuff()));
    14. _instance->initStuff();
    15. _instance->start();
    16. }
    17.  
    18. void Manager::start() {
    19. if(mStuff.size() > 0) {
    20. instance()->processStuff();
    21. } else {
    22. emit emptyQueue();
    23. }
    24. }
    25.  
    26.  
    27. void Manager::initStuff()
    28. {
    29. //if there is stuff to put in the mStuff -- put it in there
    30. }
    31.  
    32.  
    33. void Manager::processStuff()
    34. {
    35. //here is where process stuff and empty the Stuff queue
    36. //when queue is empty we emit
    37. emit emptyQueue();
    38. }
    39.  
    40. Manager* Manager::instance()
    41. {
    42. if (_instance == 0) {
    43. _instance = new DSManager();
    44. }
    45. return _instance;
    46. }
    47. Manager* Manager::_instance;
    To copy to clipboard, switch view to plain text mode 
    main.cpp — relevant code
    Qt Code:
    1. Manager::init();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Signal is emtted, slot does not fire.

    How many times would you expect it to be called? Are you running the event loop in the thread receiving the signal?

    By the way, you're not protecting your Manager object from concurrent access from within different threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Signal is emtted, slot does not fire.

    Sorry, yes, I omitted the thread safe stuff. I do have mutexes in there for that...

    I expect it to run every few minutes -- it is polling for changes. I was trying to distill the problem down to its necessities which means I put the call to
    Qt Code:
    1. _instance->start();
    To copy to clipboard, switch view to plain text mode 
    in the init function.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,690
    Thanks
    13
    Thanked 1,599 Times in 1,527 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Signal is emtted, slot does not fire.

    You create an instance of Manager in init(), connect it to the slot, and call start(). Seeing that the list is empty it emits the signal once and then terminates. There is nothing in your code that would cause a loop or repeated execution of start(). You seem to be expecting some sort of looping to occur as a result of invoking QThread.

    You do not need threading to do something like poll for changes every couple of minutes and it only complicates matters.

Similar Threads

  1. Replies: 8
    Last Post: 7th November 2012, 15:10
  2. QSqlTableModel OnFieldChange causes what signal to fire?
    By scott_hollen in forum Qt Programming
    Replies: 0
    Last Post: 27th September 2011, 21:23
  3. Replies: 2
    Last Post: 3rd May 2011, 21:22
  4. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 11:00
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 19: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.