PDA

View Full Version : Signal array? [SOLVED]



KShots
16th April 2007, 18:54
Is such a thing possible? I'd like to have an arbitrary plugin that has a vector of signals, which would directly connect to a large number of slots on stuff that it would know nothing about.

Think of it like this:

+-----------+
| | +----------+
| |-----| |
| Object A | | Object B |
| | | |
| | +----------+
+-----------+
|
|
+----------------------+
| |
| Objects C,D,E,F,G... |
| |
+----------------------+Basically, object A initializes a plugin (Object B), which actually acts as an input device to directly activate an "activate" slot on an a single plugin from an arbitrary-sized number of other plugins (C, D, E, F, G...). Object A would make the connections, then let the signals from B reach the appropriate slot in a given destination.

Unfortunately, I cannot think of a way to describe this using the Qt syntax for signals and slots. Is such a thing possible?

marcel
16th April 2007, 19:00
I believe it could be possible. After all, connect/disconnect take (const char*) as signal/slot names, so you should have some default names for your signals/slots.

Could you explain more in detail what you're trying to achieve? Maybe a more particular example...

jpn
16th April 2007, 19:06
I'm not sure if I understood the explanation but QMetaObject let's you iterate through signals and slots.

KShots
16th April 2007, 19:16
I believe it could be possible. After all, connect/disconnect take (const char*) as signal/slot names, so you should have some default names for your signals/slots.

Could you explain more in detail what you're trying to achieve? Maybe a more particular example...

Ok, I'll try and give a more specific example.

Imagine Object B is a voice activation input plugin, which would translate voice input to activate a bunch of plugins (C, D, E, F, G...) on demand, like a GPS, radio, CD player, DVD player, etc.

Object A will look at what Object B understands by polling Objects C, D, E, F, G... for a unique tag, which will match a specific signal from B and connect as many signals that it can output (each signal is a unique activation command) directly to the appropriate plugin (Object C, D, E, F, G... if available). Basically, I'm trying to find a way to do this that will not require a re-compile of the whole project every time I add a new signal.

EDIT: I think I may see a way via QMetaObject (thanks jpn)...

Object A can parse all methods from Object B, looking specifically for signals:

for(int index(0); index < objectB->metaObject()->methodCount(); index++)
{
if(objectB->metaObject()->method(index).methodType() == QMetaMethod::Signal)
{
// Match to a plugin from Object C,D,E,F,G...
// The signal signature can be obtained from objectB->metaObject()->method(index).signature()
}
}

marcel
16th April 2007, 19:33
Ok, how's this:

( this happens in A, which presumably has access to B and also to C, D ... )


for( int i = 0; i != plugins.count(); ++i )
{
if( plugins[i]->tag() == objectB->currentTag() )
connect( objectB, SIGNAL( activatePlugin() ), plugins[i], SLOT( activate() ) );
}
Is this too simple? Does B has more than one signal that it needs to connect to more than one object?
Maybe this works:


for( int i = 0; i != plugins.count(); ++i )
{
if( plugins[i]->tag() == objectB->currentTag() )
connect( objectB, SIGNAL( objectB->currentTag()->toAscii()->constData() ), plugins[i], SLOT( activate() ) );
}
Here I assume that currentTag() returns a QString that also matches a signal name from B.


For example if C = GPS and D = radio and E = GPS and currentTag() = GPS, then, GPS() signal from B will be connected to the activate() slots from C and E.

KShots
16th April 2007, 19:59
Hmm... I think that could also work. In this case, I'd have to define my signal signatures in some sort of function (objectB->currentTag()). In my case above, I can use QMetaObject to find the signals for me (removes some margin of error), but I'd still have to match the tags together correctly.

Thanks!