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
Developer
simon_hain
Posts: 15,872
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany
Accepted Solution

DropDown: Discern between programmatic and user change

I have a DropDown with a few options, the selected shows the current status.

As the status can change programmatically i have to update the DropDown.

I would like to discern between this programmatic change and a user-driven change.

 

Any idea how i could achieve this?

----------------------------------------------------------
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
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: DropDown: Discern between programmatic and user change

The conventional solution to this problem is to add a flag (boolean property) which you control, and which inhibits the onThingChanged response when it's set. You set this just before making a programmatic change, and clear it just after. When the user makes the change, the flag will be clear and the onThingChanged would proceed to do its thing.

There may be other ways to do this in specific cases but this approach has worked in all situations in which I've tried it so far.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
simon_hain
Posts: 15,872
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: DropDown: Discern between programmatic and user change

thanks, that was my idea as well, i thought there would have to be a "proper" way to do it :smileyhappy:
----------------------------------------------------------
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
Please use plain text.
Developer
strobejb
Posts: 267
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: DropDown: Discern between programmatic and user change

[ Edited ]

It is a shame that Qt works this way (i.e. programmatic changes cannot be distinguished from user changes)

 

Just realised that I didn't read your question properly so the answer below does not directly answer your question. Leaving it in place in case it is useful, or perhaps does answer usefully in some way. (Are you trying to ignore programmatically-generated signals by any chance?)

 

-----

I find it easier to block signals temporarily on an object, and then re-enable them after changing the property. Normally I expose a 'blockSignals' function on my application object (from C++) and use that from within QML.

 

i.e. in C++:

 

Q_INVOKABLE bool MyApp::blockSignals(QObject *obj, bool block)
{
   return obj ? obj->blockSignals(block) : false;
}

 and in QML:

DropDown {
  id: myDropDown

  onSelectedIndexChanged: {
     // will only be called when the user changes the selection
  }
}

function changedropdown() {

  // prevent the dropdown from sending signals
  _myApp.blockSignals(myDropDown, true);

  // change the selected index. selectedIndexChanged signal will NOT be sent
  myDropDown.selectedIndex = 123;

  // re-enable signals
  _myApp.blockSignals(myDropDown, false);
}

 

Although this method relies on the blockSignals function being exposed from C++, it does simplify the QML code quite a bit - I personally find it 'cleaner' to do it this way, vs. keeping track of things manually in qml.

Please use plain text.
Developer
simon_hain
Posts: 15,872
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: DropDown: Discern between programmatic and user change

I used blockSignals as well, but did not think on exposing it via Q_INVOKABLE, good idea.
It's a bit of a brutal solution, but the bool flag is not really the sophisticated way either :smileyhappy:
----------------------------------------------------------
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
Please use plain text.