Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to load a QML view from a singleton C++ class within a main window engine

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

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlc++interfaceloadersingleton
3 Posts 2 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    notalocal
    wrote on 11 Jan 2017, 21:41 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jiancaiyang
      wrote on 12 Jan 2017, 02:10 last edited by jiancaiyang 1 Dec 2017, 02:10
      #2

      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.
      Usage:
      
        // First, define the singleton type provider function (callback).
        static QJSValue example_qjsvalue_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
        {
            Q_UNUSED(engine)
      
            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.

      我们自己的论坛:http://qtdream.com
      擅长三维角色仿真动画。

      1 Reply Last reply
      1
      • N Offline
        N Offline
        notalocal
        wrote on 12 Jan 2017, 21:00 last edited by
        #3

        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.

        1 Reply Last reply
        1

        1/3

        11 Jan 2017, 21:41

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved