[Interest] doing bindings in C++

Zsombor Egri zsombor.egri at canonical.com
Tue Jul 29 20:11:50 CEST 2014


Hi guys,

I'm creating a component in C++ which when used should automatically be
bound to parent's width and a given height provided by a function. The
component is made in C++ due to performance reasons, so when used in a
Flickable, creating of 1000 items is done instantly.

So in short I'd need to implement something which in QML would look like
the following:
Item {
    width: parent.width
    height: units.gu(6) // 6 Grid units, Ubuntu Touch specific
}

Now, I tried to use QQmlBinding(string, source, context) (i.e.
QQmlBinding("width", source, qmlContext(source)) ) to get the binding, and
it works like charm, but creating 10000 of these items costs me >2 seconds,
whereas the same amount of QQuickItem creation costs ~90 msecs. If I do the
binding in QML, the payload is around 20-30 msecs in addition to the
previous one. I checked the KDAB blog [1] and tried to dump the bindings,
where I saw both for width and height there is a STORE_BINDING mentioned,
which according to [1] it is a QQmlBinding. However for some reason the
binding from QML is way faster than mine one. A code snippet for the width
I'm using looks as follows:

QQmlBinding *newBinding = new QQmlBinding("width", source,
qmlContext(source));
QQmlProperty target(this, "width", qmlContext(this));
newBinding->setTarget(newBinding);
QQmlAbstractBinding *prevBinding = QQmlBindingPrivate::setBinding(target,
newBinding);
if (prevBinding)
    prevBinding->destroy();

Callgrind sais most of the time is spent in the qmlBinding() function, so I
assume parsing the "width" is the one which takes the time.

Is there a better way to do these kind of property bindings in C++? Could
anyone point me to some code/blog where this kind of things are discussed?
The blog from [1] is pretty old, and I can no longer find the other two
bindings it mentions anymore.

Thanks!

Cheers,
Zsombor

[1] http://www.kdab.com/qml-engine-internals-part-3-binding-types/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140729/c57af7ad/attachment.html>


More information about the Interest mailing list