On Tue, Apr 29, 2014 at 11:32 PM, Rutledge Shawn
<[email protected]> wrote:
>
> On 29 Apr 2014, at 7:51 PM, Alan Alpert wrote:
>
>>> (3) Document that accessing ids from other .qml files without any interface 
>>> (just relying on the fact that they are in the context) creates hard to 
>>> maintain QML code.
>>
>> Agreed (honestly, it should already be there, I guess
>> https://bugreports.qt-project.org/browse/QTBUG-20453 was never
>> finished...).
>>
>> Again, we have some rough plans for a ".pragma strict". Which should ban 
>> this.
>
> What exactly do you want to ban and what are the best alternatives?  "Without 
> any interface" implies there is a right way to share.

Correct answer: Define an interface. Probably best explained via the
attached examples: "Bad" has no interface defined for the object
reference, "Good" does.

Runner up: Singletons. When you can't or don't want to pass references
to individual object instances, you can pass it to a shared interface
instance and use that. Which is QML singletons, and is fairly
convenient already (though there are known issues like QTBUG-34418
which can make it less convenient than it should).

Workaround: Shared JS file. Does most of the same stuff as the
singleton (biggest exception: QTBUG-21844 ), and is available in
earlier versions. Should singletons be available, use them instead as
they have the declarative and typed interface benefits.

> In practice I'm often not sure about the scoping rules, so when something 
> that I think should be accessible is not, I'm often not sure if that is a bug 
> but just start looking for another way to do it (which sometimes can be 
> clumsy).

If an id is not in file scope, then it's either a bug or an ugly
hidden feature that we keep around for convenience but you aren't
supposed to use ;) .

--
Alan Alpert
import QtQuick 2.1

MouseArea {
    onClicked: anItem.color = "blue"
}
import QtQuick 2.1

MouseArea {
    property Rectangle target
    onClicked: target.color = "blue"
}
import QtQuick 2.1

Rectangle {
    id: anItem
    color: "red"
    width: 200
    height: 200
    BadUser {
        anchors.fill: parent
    }
}
import QtQuick 2.1

Rectangle {
    id: anItem
    color: "red"
    width: 200
    height: 200
    GoodUser {
        target: anItem
        anchors.fill: parent
    }
}
_______________________________________________
Development mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/development

Reply via email to