Signal is emitted, slot is not running
-
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();
@ -
Did you place a break point on
@
emit emptyQueue()
@
Did you check the return value of connect is true?
Was there an output concerning the connect?You do not specify the on what system you are running and what your IDE is.
Is it qt creator? There you should see outputs in the application output window. -
yes, it hit that breakpoint.
yes, the connect returns true.
no output concerning the connect on run or build.
I am using Qt5.0.2 and qtcreator on Win7.
-
even thought the connect is returning true, it isn't behaving like you want it to. Why don't you just call initStuff instead of having a signal for it?
-
@void Manager::start() {
if(mStuff.size() > 0) {
instance()->processStuff();
} else {
initStuff();
}
}@and
@void Manager::processStuff()
{
//here is where process stuff and empty the Stuff queue
//when queue is empty we emit
initStuff();
}@
workbut looking at your code you call initStuff at the end of processStuff and in the else section of start().
Why not just
@void Manager::processStuff()
{
//here is where process stuff and empty the Stuff queue
}
void Manager::start() {
if(mStuff.size() > 0) {
instance()->processStuff();
}
initStuff();
}
@