Reputation: 30933
i try to invoke Slot in thread object when threas started but getting this error:
Object::connect: No such slot Worker::doWork(pFoo)
the thread executing code:
// main class
m_WorkerThread = new QThread();
FooStack* pfooStack = InternalStorageManager::getInstance()->getStack();
m_Worker = new Worker();
bool done = connect(m_WorkerThread,
SIGNAL(started()),
m_Worker,
SLOT(doWork(pfooStack)));
m_Worker->moveToThread(m_WorkerThread);
m_WorkerThread->start();
// class Worker
// cpp imple
void Worker::doWork(FooStack *& rp_urlsStack)
{
}
// header
class Worker : public QObject
{
Q_OBJECT
public :
Worker();
~Worker();
public slots:
void doWork(FooStack *&);
};
Upvotes: 0
Views: 2758
Reputation: 22366
Object::connect: No such slot Worker::doWork(pFoo)
You can't pass objects in connection declarations.
Can't you pass pfooStack
into the Worker
constructor?
EDIT:
class Main : ...
{
...
void startThread(); // The method in your example.
private slots:
void startWork();
...
};
void Main::startThread()
{
m_WorkerThread = new QThread();
m_Worker = new Worker();
bool done = connect(m_WorkerThread, SIGNAL(started()),
this, SLOT(startWork()));
m_Worker->moveToThread(m_WorkerThread);
m_WorkerThread->start();
}
void Main::startWork()
{
m_Worker->doWork(InternalStorageManager::getInstance()->getStack());
}
Upvotes: 3
Reputation: 62898
You can't do it that way, you can't pass current variables as slot method parameters in connect, and slot can't have more parameters than the signal. In addition to other answers, you can achieve this with QSignalMapper, but if you have just one connection to the slot, that seems like an overkill.
If you can use Qt5 and C++11, then you can connect signal to lambda functions, not just slots, but I'm not absolutely sure if that supports creating a closure (that is, using the local variable in the lambda function, which you would need here).
Upvotes: 1
Reputation: 3213
I have not compiled the code on my computer, but it should imply what you need:
m_WorkerThread = new QThread();
FooStack* pfooStack = InternalStorageManager::getInstance()->getStack();
m_Worker = new Worker(pfooStack);
bool done = connect(m_WorkerThread,
SIGNAL(started()),
m_Worker,
SLOT(doWork()));
m_Worker->moveToThread(m_WorkerThread);
m_WorkerThread->start();
void Worker::doWork()
{
//use stack here
}
class Worker : public QObject
{
Q_OBJECT
public :
Worker(FooStack *& rp_urlsStack):stack(rp_urlsStack);
~Worker();
public slots:
void doWork();
private:
FooStack*& stack;
};
Upvotes: 1
Reputation: 1099
I think you need to change the signal and slot signatures. From the QT Documenation:
The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro.
Upvotes: 0