[Interest] Problem with unintentional circular dependencies between QML singletons

Elvis Stansvik elvstone at gmail.com
Wed Mar 30 11:00:11 CEST 2016


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?

Cheers,
Elvis

[1] https://bugreports.qt.io/browse/QTBUG-39703
[2] https://bugreports.qt.io/browse/QTBUG-34418



More information about the Interest mailing list