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
Highlighted
Developer
Posts: 230
Registered: ‎01-12-2013
My Device: z30
My Carrier: Mobile Vikings
Accepted Solution

QML Binding

Hey Developers

 

I was looking for a way to bind the value of a textfield to a property of a C++ class. This way I get a nice seperation between view and model.

 

I found that it should be possible with the binding element

https://developer.blackberry.com/cascades/reference/qml-binding.html

 

But when I try to use the Binding { } element in QML, he gives me the error that it does not exist... I have the 10.1 SDK installed so I think that it should work with that one.

 

Thanks in advance

______________________________________________________
BB10-OAuth: GitHub
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: QML Binding

Can't you set the exposed C++ property or method in some sort of onUpdate method? Or send a signal when the texfield's value is changed?

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 230
Registered: ‎01-12-2013
My Device: z30
My Carrier: Mobile Vikings

Re: QML Binding

I'm doing that right now. But I think that with binding, the code is more loosely coupled and becomes cleaner. As you allready now, I'm looking into the MVVM pattern in cascades/QML. This would perfectly fit I guess...

______________________________________________________
BB10-OAuth: GitHub
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: QML Binding

[ Edited ]

Binding {} is a part of QtQuick.

If you add:

import QtQuick 1.0

Then the following code will load without errors:

attachedObjects: [
Binding {}
]

If you make it work, please share with us.


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 230
Registered: ‎01-12-2013
My Device: z30
My Carrier: Mobile Vikings

Re: QML Binding

Thanks for the tip! I managed to make things work. To bad the Binding element only provides one-way binding. Maybe I should create a ticket on the Qt forums Smiley Wink.

 

The code can be viewed at https://github.com/SamVerschueren/BB10-MVVMLoginExample

 

It's just the start of my MVVM Example and I will see how far I can go.

______________________________________________________
BB10-OAuth: GitHub
Developer
Posts: 16,997
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: QML Binding

you can also use Q_PROPERTY with a NOTIFY slot i think
see http://doc.qt.digia.com/4.7/properties.html
----------------------------------------------------------
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: 230
Registered: ‎01-12-2013
My Device: z30
My Carrier: Mobile Vikings

Re: QML Binding

I use the notify slot to notify the view. But it would be more clean that I don't have to set the TextFields text property to viewModel.text.

 

I have to do:

TextField {
    id: txtUsername
    text: loginVM.username
    hintText: qsTr("Username")
}
...
Binding {
    target: loginVM
    value: txtUsername.text
    property: "username"
}

 It would be very nice if I can only do this:

TextField {
    id: txtUsername
    hintText: qsTr("Username")
}
...
Binding {
    target: loginVM
    value: txtUsername.text
    property: "username"
}

 

______________________________________________________
BB10-OAuth: GitHub