问题
So I was finally able to reproduce it in an MCVE:
// S_Settings.qml
pragma Singleton
import QtQuick 2.7
Item {
property real bsize: 50
}
// in main()
qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/S_Settings.qml")), "Set", 1, 0, "SS");
// main.qml
import QtQuick 2.7
import QtQuick.Window 2.2
import Set 1.0
Window {
id: main
visible: true
width: 300
height: 200
Rectangle {
anchors.centerIn: parent
width: SS.bsize * 4
height: SS.bsize * 2
radius: SS.bsize * .5
color: "red"
border.color: "black"
border.width: SS.bsize * .1
}
}
This produces the expected result:
Now changing things a bit:
// S_Settings.qml
pragma Singleton
import QtQuick 2.7
Item {
property real bsize: 50
property real tenth: bsize * .1
property real half: bsize * .5
}
// main.qml
import QtQuick 2.7
import QtQuick.Window 2.2
import Set 1.0
Window {
id: main
visible: true
width: 300
height: 200
Rectangle {
anchors.centerIn: parent
width: SS.bsize * 4
height: SS.bsize * 2
radius: SS.half
color: "red"
border.color: "black"
border.width: SS.tenth
}
}
This in my particular case happens to produce no visible difference, unlike in the more complex production code, where it immediately produced visible bugs, seemingly everything is OK, but only seemingly.
Then, in the singleton QML file, push the last line one line up (so that half
comes before tenth
), and the result is as follows:
Push it another line up, and the result:
The result is all messed up now. In this small example it only caused the property values to corrupt, in my production code on top of that it also caused properties types to corrupt, causing the QML engine to see color
s as int
s, string
s as bool
s, int
s as null
s and similar crazy stuff.
So, is it reproducible? Any idea what's going on?
Update: Apparently this is one of the issues which plague the usage of the qml cache compiler.
A viable option to avoid the whole issue is to simply disable the cache via qputenv("QML_DISABLE_DISK_CACHE", "true");
first thing in main()
.
来源:https://stackoverflow.com/questions/41903160/presence-and-location-of-a-property-in-a-qml-singleton-source-results-in-arbitra