Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 20
Registered: ‎03-23-2014
My Device: z10
My Carrier: fido

How to listent to 2 signals at once?

How can i make a slot executed when 2 signals emited for example:

 

signals:

void teaReady(Tea*);

void cakeReady(Cake*);

 

slots:

void eat(Tea *, Cake *);

 

 

I want eat() to be executed only when both teaReady() and cakeReady() are emited because eat() need the params passed by both teaReady and cakeReady()

 

Hope to get some help

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to listent to 2 signals at once?

The easiest way to do it is to store the pointers within that class with what's called a dirty bit, a boolean value indicating that the value has been updated. Then when both dirty bits are set call your slot.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
New Developer
Posts: 20
Registered: ‎03-23-2014
My Device: z10
My Carrier: fido

Re: How to listent to 2 signals at once?

But how can i know when the 2 bits are set? Can you give an example?

Thanks

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How to listent to 2 signals at once?

I'm not at my computer at the moment so it would be hard for me to do that.

 

However if you do a search on Q_PROPERTY in the documentation you'll see you can do an onChange signal, define those as simple functions that change the associated dirty bit and if both are set fire the eat signal, clearing the bits within that function obviously.

 

Do a search on Signals and slots with the documentation if you are still confused about that part of it to.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: How to listent to 2 signals at once?

[ Edited ]

//this is just for simple understanding. this is no working code, because I haven't written Cpp in a while

 

 

//define two additional variables in your .h

Tea* preparedTea;
Cake* preparedCake;

//in your *.m, connect your signals to two internal (!) slots 
teaReady() --> onTeaReady()
cakeReady() --> onCakeReady()

//then, in both teaReady / cakeReady functions, check if the other one is available

function onTeaReady(Tea* tea) {
    if (preparedCake != NULL) {
        //trigger the eat-slot with the prepared cake

    } else {
//store the tea and do nothing else
preparedTea = tea; } }

 

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
New Developer
Posts: 20
Registered: ‎03-23-2014
My Device: z10
My Carrier: fido

Re: How to listent to 2 signals at once?

Thanks, I got the idea now but I was hoping to have something easier since cross checking multiple val sound like lot of work. In my case I only need 2 var for the 2 signals but if I have 20 signals then the code can quickly become messy
Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: How to listent to 2 signals at once?

yes, correct. I often use this code when I need to wait for 2 signals, sometimes even for three. But if you would have more, I'd suggest creating another class for that which handles each dish inside an array or a list. Example: "DiningTable"

 

DiningTable* diningTable;

connect(diningTable, everythingIsReady(), this, eat()); //everythingIsReady fires when all dishes have been added

//I'd set flags what the dining table should wait for
diningTable->waitFor(CAKE, TEA, BISCUITS); //simple int-flags

//then, register all dish-creating objects
connect(teaMaker, onDishReady(), diningTable, addDish());
connect(cakeMaker, onDishReady(), diningTable, addDish());
connect(coffeeMaker, onDishReady(), diningTable, addDish());
connect(biscuitMaker, onDishReady(), diningTable, addDish());
//etc. Simpler: Each maker should derive from a common dish-making class, then you could loop this

 

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Highlighted
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to listent to 2 signals at once?

I would suggest to use Inheritance. Define the slot with a super class that both used classes extend, you can now call it with both tea and cake.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter