I can't do this cause the object lives in different threads.
I've something like this:
{
Q_OBJECT
signals:
void invokeObjMethod(int);
private:
void initManager(){
for ( int idx = 0 ; idx < n; idx++){
MyObject * obj = new MyObject(idx);
connect(this,SIGNAL(invokeObjMethod(int)),obj,SLOT(doSomething(int)));
obj->moveToThread(th);
th->start();
}
}
}
{
Q_OBJECT
public:
MyObject
(int idx,
QObject * parent
= 0) m_index(idx){}
private:
int m_index;
public slots:
void doSomething(int idx){
if ( idx != m_index)
return;
...do Something ...
}
}
class Manager : public QObject
{
Q_OBJECT
signals:
void invokeObjMethod(int);
private:
void initManager(){
for ( int idx = 0 ; idx < n; idx++){
QThread * th = new QThread(this);
MyObject * obj = new MyObject(idx);
connect(this,SIGNAL(invokeObjMethod(int)),obj,SLOT(doSomething(int)));
obj->moveToThread(th);
th->start();
}
}
}
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject(int idx, QObject * parent = 0)
:QObject(parent),
m_index(idx){}
private:
int m_index;
public slots:
void doSomething(int idx){
if ( idx != m_index)
return;
...do Something ...
}
}
To copy to clipboard, switch view to plain text mode
When the manager emit the signal invokeObjMethod(int) all the objects invoke their slot but only one do something.
I want to know if this is the right way or there are other best solu
Bookmarks