3

I'm new to Qml and having some trouble connecting a javascript handler to a property's signal. I have a C++ object with a property and signal.

class CppObject : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QVariant value READ getValue WRITE setValue NOTIFY valueChanged)

signals:
    void valueChanged(const QVariant &);
};

The objects are created through a C++ factory method and I'm able to bind the values and changes to Qml properties. This all works.

property CppObject obj: cppProxy.PropertyFactory("foo");

Text
{
    x: 100;
    y: 100;
    text: parent.obj.value;
}

For some properties, I'd like to connect the valueChanged signal to a javascript function. I've been up and down through the Qml documentation and have tried a bunch of stuff without any luck. I figured something like this should work, but doesn't

function objEventHandler()
{
    console.log('objEventHandler() ran')
}

Component.onCompleted:
{
    obj.value.valueChanged.connect(objEventHandler);
}

What is the best way to do this?

2 Answers 2

5

You can also connect as you've tried in your example, but the form is:

Component.onCompleted:
{
    obj.valueChanged.connect(objEventHandler);
}

The signal is not a property of the 'value' object, but of 'obj'.

Sign up to request clarification or add additional context in comments.

1 Comment

One additional complication I came across is that "this" is not always well defined in signal handlers if you're getting a signal from C++. So, in this example I had to use a closure to retain access to the "obj" context, like this: Component.onCompleted: { var o = obj; obj.valueChanged.connect(function() { console.log(o.text); }); }
2

It's simple using the connections object.

Connections
{
    target: obj;
    onValueChanged: console.log('changed');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.