Re: One signal more slots
You can just keep a pointer of all objects in the manager and use a QHash using the id parameters to make a key
Code:
struct Object
{
void bar(){/* do nothing */}
};
class Manager
{
QHash<Key, Object*> map;
void foo()
{
// do something with 'key's object:
Key key;
map[key]->bar(); // no need to run method for each key/object
}
};
Re: One signal more slots
I can't do this cause the object lives in different threads.
I've something like this:
Code:
{
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 ...
}
}
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
Re: One signal more slots
Re: One signal more slots
The QSignalMapper class bundles signals from identifiable senders.
In my case I've only one sender.
I think that is not possible to use QSignalMapper in my case.
Am I wrong?
Re: One signal more slots
You are right. QSignalMapper cannot help you here.
There is a simple solution to your problem: QMetaObject::invokeMethod(). No more indices, just do something like
Code:
QMetaObject::invokeMethod(pointerToTheMyObjectInstance,
"doSomething", Qt
::QueuedConnection);
Re: One signal more slots
With this method I can directly call the thread slot of the single object, with QueuedConnection (safety for thread).
I think it's the best solution....many thanks!!!
Re: One signal more slots
Quote:
Originally Posted by
paolom
I can't do this cause the object lives in different threads.
Yes you can, just use invokeMethod and use it with a queued connection slot.