[Interest] Awkward Javascript problem

Nuno Santos nunosantos at imaginando.pt
Thu Nov 30 12:20:49 CET 2017


Mark,

I have found that the object actually exists but when I make two subsequent calls, it fails on the seconds. Look at this:

// not working

MouseArea {
    anchors.fill: parent
    onClicked: {
    	console.log("controller: " + controller);
        console.log("here1");
        controller.kernel.setParameter(DRCSynthEngine.KeyboardScale, 22)
        console.log("here2");
        controller.kernel.setParameter(DRCSynthEngine.KeyboardScaleCustom, controller.kernel.keyboard.toggleKey(index))
        console.log("here3");
    }
}

// ouput

qml: controller: DRCController(0x7fff5c1f99f0)
qml: here1
qml: here2
qrc:/qml/PanelPerform.qml:131: ReferenceError: controller is not defined

// working

MouseArea {
    anchors.fill: parent
    onClicked: {
    	console.log("controller: " + controller);
        var obj = {};

        obj[DRCSynthEngine.KeyboardScale]=22;
        obj[DRCSynthEngine.KeyboardScaleCustom]=controller.kernel.keyboard.toggleKey(index);

        console.log("here1");
        controller.kernel.setParameters(obj)
        console.log("here2");
    }
}

// outputqml:

qml: controller: DRCController(0x7fff5b96a9f0)
qml: here1
qml: here2

So, the workaround was to have a method to set two parameters at one, using only one call to controller instance.

Sometimes I find awkward behaviours on qml which I don’t how to explain…

Regards,

Nuno

> On 30 Nov 2017, at 11:11, Mark Tucker <mark.tucker at airborne.aero> wrote:
> 
> Hi Nuno,
>  
> Are you attempting to access this "controller" property within a "Component.onCompleted" block of code?
>  
> With Loaders I've found from experience that there's a brief period of time where the component has been created (and therefore its Component.onCompleted signal fires), but it has not yet been fully added to the object heirarchy, so attempting to access anything outside of that object's own scope fails.
>  
> The only way I've ever found to get around this limitation is to instead figure out a way of using the onLoaded signal from the Loader, which seems to be emitted at a point where the QML object is correctly placed within the object heirarchy and can actually correctly access things outside of its own scope.
>  
> The above is my own observations from a number of years of writing QML code - it may be what you're seeing here. I don't know anywhere near enough about how the C++ side of things or how the internals of Qt works to be able to provide any more detail.
>  
> Perhaps you can change the point at which you run this code?
>  
> Mark T
>  
> From: Interest [mailto:interest-bounces+mark.tucker=airborne.aero at qt-project.org] On Behalf Of Nuno Santos
> Sent: 30 November 2017 10:11
> To: Qt Project MailingList <interest at qt-project.org>
> Subject: [Interest] Awkward Javascript problem
>  
> Hi,
>  
> I usually register a controller property that points to a Controller classe when I start my Qt apps. I can then access the controller from any point of the code in Javascript:
>  
> quickView->rootContext()->setContextProperty("controller", &controller);
>  
> Curiously, I’m having an issue in which inside a MouseArea onClick event, controller is not defined:
>  
> ReferenceError: controller is not defined 
>  
> This MouseArea lives inside a Loader component that is one of the children of a SplitView. 
>  
> Maybe this be the cause for such ReferenceError? How can I workaround this? Any ideas?
>  
> Thanks!
>  
> Regards,
>  
> Nuno

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171130/2e6d0687/attachment.html>


More information about the Interest mailing list