Qt World Summit: Submit your Presentation

How to load a QML view from a singleton C++ class within a main window engine

  • Hey guys,

    So I am creating my first application using QML and attaching it to C++ for some background work. In essence I am trying to create an MVC, where the View portion is a C++ interface Singleton attached to a QML view, which ideally is also a singleton, such that if I have multiple of these views (where different control options are available) I can load one or the other from the main window (as a subview) without having to reattach all the signals, etc.

    Is this possible? If so can you please provide me with documentation/a starting point/example to look at, because what I did previously (with a Loader component having its source set by different buttons being pressed) lead me to spawning new and separate QML components, which did not function as if they did as separate views instantiated by the C++ Singleton itself.

    Sorry for the noob question, I'm brand new to QML/JS!

  • This is digested by Qt Quick documentation:

    int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName, QJSValue(* ) ( QQmlEngine *, QJSEngine * ) callback)
    This function may be used to register a singleton type provider callback in a particular uri and typeName with a version specified in versionMajor and versionMinor.
    Installing a singleton type allows developers to provide arbitrary functionality (methods and properties) to a client without requiring individual instances of the type to be instantiated by the client.
    A singleton type may be either a QObject or a QJSValue. This function should be used to register a singleton type provider function which returns a QJSValue as a singleton type.
    NOTE: QJSValue singleton type properties will not trigger binding re-evaluation if changed.
      // First, define the singleton type provider function (callback).
      static QJSValue example_qjsvalue_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
          static int seedValue = 5;
          QJSValue example = scriptEngine->newObject();
          example.setProperty("someProperty", seedValue++);
          return example;
      // Second, register the singleton type provider with QML by calling this function in an initialization function.
      qmlRegisterSingletonType("Qt.example.qjsvalueApi", 1, 0, "MyApi", example_qjsvalue_singletontype_provider);
    In order to use the registered singleton type in QML, you must import the singleton type.
      import QtQuick 2.0
      import Qt.example.qjsvalueApi 1.0 as ExampleApi
      Item {
          id: root
          property int someValue: ExampleApi.MyApi.someProperty

    Continue reading by searching qmlRegisterSingletonType as keyword.

  • I appreciate your response, it lead me to a lot of research on the topic! I figured out it was much easier to implement a tabview on the main view and then to connect the signals with the child objects which were themselves individual views.