[Interest] Problem with unintentional circular dependencies between QML singletons

Ben Lau xbenlau at gmail.com
Wed Mar 30 11:54:48 CEST 2016


Hi Elvis,

It is not allowed in QML. However, you may pass the instance of State to
MyItem as var type during construction.

On 30 March 2016 at 17:33, Elvis Stansvik <elvstone at gmail.com> wrote:

> Hi Ben,
>
> 2016-03-30 11:19 GMT+02:00 Ben Lau <xbenlau at gmail.com>:
> >
> > I usually have many singleton components in my app. I would break them
> down
> > into different packages to avoid circular dependence.
>
> Yes, I realized that this is a possible workaround: If I would move
> the Style.qml in my example into a package "styles" and do import
> "styles" instead of import "." in MyItem.qml, it would work without
> problems.
>
> But what about my latest message, what if MyItem needs to access some
> state in the State singleton, but the State singleton also needs to
> have a MyItem property (as part of the state). Would that be in any
> way possible, or is it simply not allowed? The more I think about it
> the more I realize that's probably the case.
>
> Elvis
>
> >
> > On 30 March 2016 at 17:06, Elvis Stansvik <elvstone at gmail.com> wrote:
> >>
> >> 2016-03-30 11:00 GMT+02:00 Elvis Stansvik <elvstone at gmail.com>:
> >> > Hi all,
> >> >
> >> > I have a problem that I think is due to QTBUG-39703 [1] and
> >> > QTBUG-34418 [2], but perhaps there's a smart workaround.
> >> >
> >> > Imagine the following setup:
> >> >
> >> > dir/
> >> >     main.qml           The main file
> >> >     MyItem.qml       Some item, uses Style
> >> >     Style.qml           Some colors et.c.
> >> >     State.qml           Some application-wide state (uses MyItem, see
> >> > below!)
> >> >     qmldir
> >> >
> >> > with contents as follows:
> >> >
> >> >
> >> > //// main.qml
> >> > import QtQuick 2.4
> >> >
> >> > MyItem {
> >> > }
> >> >
> >> >
> >> > //// MyItem.qml
> >> > import QtQuick 2.4
> >> > import "." // required due to QTBUG-34418
> >> >
> >> > Rectangle {
> >> >     color: Style.backgroundColor
> >> > }
> >> >
> >> >
> >> > //// Style.qml  --  Some colors et.c.
> >> > pragma Singleton
> >> >
> >> > import QtQuick 2.4
> >> >
> >> > QtObject {
> >> >     readonly property color backgroundColor: "#abc"
> >> > }
> >> >
> >> >
> >> > //// State.qml  --  Some application-wide state.
> >> > pragma Singleton
> >> >
> >> > import QtQuick 2.4
> >> >
> >> > QtObject {
> >> >     property int someState: 1  // This is fine.
> >> >
> >> >     // But what if we need to keep a MyItem as state
> >> >     // like below? It would lead to a process exit with
> >> >     // status 255 because this singleton will implicitly
> >> >     // import MyItem.qml, which in turn must import "."
> >> >     // to access the Style.qml singleton (due to
> >> >     // QTBUG-34418), and this will bring in State.qml
> >> >     // again -> circular dependency :/
> >> >
> >> >     //property MyItem someItem: MyItem {}
> >> >
> >> >     // Uncomment the above -> Process exits with 255 due to
> QTBUG-39703
> >> > }
> >> >
> >> > This last file shows the problem, if the application state in the
> >> > State.qml singleton includes a MyItem, I get a circular dependency
> >> > leading to a process exit with status 255.
> >> >
> >> > Is there some way around this conundrum? It's the requirement to make
> >> > an explicit import "." to get the Style singleton (due to QTBUG-39703)
> >> > that really spoils it I think :/
> >> >
> >> > The qmldir file is:
> >> >
> >> > singleton Style 1.0 Style.qml
> >> > singleton State 1.0 State.qml
> >> > MyItem 1.0 MyItem.qml
> >> >
> >> > Surely someone has used multiple singletons like this?
> >>
> >> And I realized that, eventhough my example did not show it, it's
> >> likely that MyItem would have a dependency on State.qml (the
> >> application-wide state singleton), so there would indeed be a circular
> >> dependency as far as imports go, but not a real circular dependency of
> >> course. That is, I would not make use of the MyItem property of the
> >> singleton from MyItem itself. Can QML not cope with such a situation
> >> currently?
> >>
> >> Elvis
> >>
> >> >
> >> > Cheers,
> >> > Elvis
> >> >
> >> > [1] https://bugreports.qt.io/browse/QTBUG-39703
> >> > [2] https://bugreports.qt.io/browse/QTBUG-34418
> >> _______________________________________________
> >> Interest mailing list
> >> Interest at qt-project.org
> >> http://lists.qt-project.org/mailman/listinfo/interest
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160330/78932def/attachment.html>


More information about the Interest mailing list