PDA

View Full Version : Signal is emtted, slot does not fire.



JasonKretzer
1st August 2013, 20:25
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

#ifndef MANAGER_H
#define MANAGER_H

#include <QObject>

class Manager : public QObject
{
Q_OBJECT
public:
static Manager* instance();
static void init();
void start();
bool hasStuff() {return mStuff.size() > 0;}
private:
explicit Manager(QObject *parent = 0);
static Manager* _instance;

QList<Channel*> mStuff;
void processStuff();
void loadStuffQueue();

signals:
void emptyQueue();

public slots:
void initStuff();
};

#endif // MANAGER_H

manager.cpp

#include "manager.h"

Manager::Manager(QObject *parent) :
QObject(parent)
{
mThread = new QThread(0);
moveToThread(mThread);
}

void Manager::init()
{
_instance = new Manager();
connect(_instance, SIGNAL(emptyQueue()), _instance, SLOT(initStuff()));
_instance->initStuff();
_instance->start();
}

void Manager::start() {
if(mStuff.size() > 0) {
instance()->processStuff();
} else {
emit emptyQueue();
}
}


void Manager::initStuff()
{
//if there is stuff to put in the mStuff -- put it in there
}


void Manager::processStuff()
{
//here is where process stuff and empty the Stuff queue
//when queue is empty we emit
emit emptyQueue();
}

Manager* Manager::instance()
{
if (_instance == 0) {
_instance = new DSManager();
}
return _instance;
}
Manager* Manager::_instance;

main.cpp — relevant code

Manager::init();

wysota
1st August 2013, 20:31
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.

JasonKretzer
1st August 2013, 21:01
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
_instance->start(); in the init function.

ChrisW67
1st August 2013, 22:40
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.