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
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
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
Developer
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!
Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
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 Smiley Happy
----------------------------------------------------------
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
Developer
Posts: 293
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.

Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
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 Smiley Happy
----------------------------------------------------------
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
Visitor
Posts: 1
Registered: ‎09-09-2014
My Device: Passport
My Carrier: Verizon

Re: DropDown: Discern between programmatic and user change

So I found I better solution than exposing a method from C++. I  don't know if this will work for everyone but it works for my case.

 

 

Assuming you have a signal like:
DropDownBox{

    id: myDropDown

    property string currentText: ""

    ...
    onCurrentTextChanged:{

         console.log("Emitted Current Text Changed");

    }

}

 

in you drop down box... you can disable or enable the object with the enabled flag and it will disable signals as well.

function setCurrentText(text){

    myDropDown.enabled = false;

    myDropDown.currentText = text;

    myDropDown.enabled = true;

}

 

Hope this helps. 

 

 

 

Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: DropDown: Discern between programmatic and user change

good idea!
----------------------------------------------------------
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