Qml doesn't allow javascript singletons?
-
I appears as though javascript files are executed in a separate context each time they are imported. This appears to make it impossible to store singleton objects at the top-level scope for the file. Is there any way to accomplish this?
Thanks!
-
I appears as though javascript files are executed in a separate context each time they are imported. This appears to make it impossible to store singleton objects at the top-level scope for the file. Is there any way to accomplish this?
Thanks!
@patrickkidd said in Qml doesn't allow javascript singletons?:
Is there any way to accomplish this?
I think what you are looking for is pragma Singleton
-
I appears as though javascript files are executed in a separate context each time they are imported. This appears to make it impossible to store singleton objects at the top-level scope for the file. Is there any way to accomplish this?
Thanks!
-
@patrickkidd said in Qml doesn't allow javascript singletons?:
Is there any way to accomplish this?
I think what you are looking for is pragma Singleton
@KroMignon said in Qml doesn't allow javascript singletons?:
I think what you are looking for is pragma Singleton
Thanks, but that is for singleton Qml components not Javascript files.
@LeLev said in Qml doesn't allow javascript singletons?:
@patrickkidd hi
https://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-librariesThat was what I was looking for. However, of my 7 or so qml files that import this (all loaded in a different
QQuickWidget
), the top-level code is still being executed twice. That's better than the 7 times it was executed before, but it should only be executed once. AllQQuickWidget
instances are using the sameQQmlEngine
so I'm not sure what's going on here. -
@KroMignon said in Qml doesn't allow javascript singletons?:
I think what you are looking for is pragma Singleton
Thanks, but that is for singleton Qml components not Javascript files.
@LeLev said in Qml doesn't allow javascript singletons?:
@patrickkidd hi
https://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-librariesThat was what I was looking for. However, of my 7 or so qml files that import this (all loaded in a different
QQuickWidget
), the top-level code is still being executed twice. That's better than the 7 times it was executed before, but it should only be executed once. AllQQuickWidget
instances are using the sameQQmlEngine
so I'm not sure what's going on here.sorry i forgot to answer
@patrickkidd said in Qml doesn't allow javascript singletons?:executed twice
I did the test couple of days ago and the code in my js file is executed only once.
How do you import the file ? -
sorry i forgot to answer
@patrickkidd said in Qml doesn't allow javascript singletons?:executed twice
I did the test couple of days ago and the code in my js file is executed only once.
How do you import the file ?@LeLev
@LeLev said in Qml doesn't allow javascript singletons?:
sorry i forgot to answer
@patrickkidd said in Qml doesn't allow javascript singletons?:executed twice
I did the test couple of days ago and the code in my js file is executed only once.
How do you import the file ?I import the file from four different qml files, all do this:
import 'js/Global.js' as Global
-
@LeLev
@LeLev said in Qml doesn't allow javascript singletons?:
sorry i forgot to answer
@patrickkidd said in Qml doesn't allow javascript singletons?:executed twice
I did the test couple of days ago and the code in my js file is executed only once.
How do you import the file ?I import the file from four different qml files, all do this:
import 'js/Global.js' as Global
@patrickkidd can you make a simple project that replicates this behavior ?