Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
How to access QML Singleton from javascript .js file?

How can javascript in a separate .js file access a QML singleton?

My javascript library wants to read configuration values stored in a singleton which is defined in TrConfig.qml:

// TrConfig.qml
pragma Singleton
import QtQml 2.0

QtObject {
  property var myWidth: 234
}

The qmldir file contains

singleton TrConfig 1.0 TrConfig.qml

This works fine from inside .qml files by simply referencing TrConfig.<fieldname>. But in .js files after .import TrConfig as TC, referencing TC.<fieldname> always returns undefined.

// myjslib.js
.import QtQml 2.11 as QtQml
.import TrConfig as TC

function myfunc() {
  console.log("In myjslib.js myWidth is " + TC.myWidth); 
  // prints "... myWidth is undefined"
}

Here is the main .qml file (run with "qml Foo.qml"):

// Foo.qml
import QtQuick 
import QtQuick.Controls 
import "myjslib.js" as J

Rectangle {
  width: TrConfig.myWidth // this works
  height: 100
  color: "red"
  
  Component.onCompleted: {
    console.log("Foo onCompleted: myWidth="+TrConfig.myWidth);
    J.myfunc();
  }
}

The output is:

qml: Foo onCompleted: myWidth=234
qml: In myjslib.js myWidth is undefined

Any help would be greatly appreciated!

[Updated 11/6/21 to add ".import TrConfig as TC" into the .js file]

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda