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. QML singleton instantiation issue
Forum Updated to NodeBB v4.3 + New Features

QML singleton instantiation issue

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 1.0k 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.
  • T Offline
    T Offline
    Tannz0rz
    wrote on last edited by Tannz0rz
    #1

    I have a simple Qt Quick project whose tree looks like this:

    main.qml
    actions/
        AppActions.qml
        qmldir
    stores/
        UserStore.qml
        qmldir
    

    Both AppActions and UserStore are marked with pragma Singleton and are marked as singletons in their corresponding qmldir files. In AppActions a signal is declared, and in UserStore a connection is made to that signal. This signal is emitted upon the receipt of onClicked in a Button in main.qml.

    At first glance, the signal is not received by UserStore because UserStore does not exist. Only upon adding a no-op call of UserStore; in main.qml is the UserStore created and the signal properly received. From what I understand, the singleton types ought to be instantiated given the presence of the qmldir files.

    Here are the relevant source files:

    AppActions.qml:

    pragma Singleton
    
    import QtQuick 2.12
    
    Item
    {
        signal login();
    }
    

    UserStore.qml:

    pragma Singleton
    
    import QtQuick 2.12
    
    import "../actions"
    
    Item
    {
        Connections
        {
            target: AppActions
    
            onLogin:
            {
                console.log("Logged in!");
            }
        }
    }
    

    main.qml:

    import QtQuick 2.0
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.5
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Window 2.3
    
    import "actions"
    import "stores"
    
    ApplicationWindow
    {
        id: appWindow
    
        visible: true
        width: 640
        height: 480
    
        Button
        {
            anchors.fill: parent
    
            font.pointSize: 20
            text: "Login"
    
            onClicked:
            {
                AppActions.login();
            }
        }
    
        Component.onCompleted:
        {
            UserStore; // If this line is removed, AppActions.login is not received by UserStore
        }
    }
    

    The link below is the project in its entirety, which also includes two trace files from the profiler: trace_good.qzt and trace_bad.qzt. In the latter trace it clearly shows that the UserStore is not properly created.

    Project: test.zip
    Build kit used: Qt 5.13.0 MSVC2017 64bit

    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by IntruderExcluder
      #2

      Singletons are created by demand only and qmldir can't help you here. Your workaround probably is the best way for your case. Maybe you should change architecture of your app, some sort of dispatcher can be useful here. Here is a good example: QML Flux.

      T 1 Reply Last reply
      0
      • IntruderExcluderI IntruderExcluder

        Singletons are created by demand only and qmldir can't help you here. Your workaround probably is the best way for your case. Maybe you should change architecture of your app, some sort of dispatcher can be useful here. Here is a good example: QML Flux.

        T Offline
        T Offline
        Tannz0rz
        wrote on last edited by Tannz0rz
        #3

        @IntruderExcluder https://github.com/benlau/quickflux/issues/26

        I had originally started down the path of this issue using QuickFlux, but the issue lay with the singleton pattern. This is how BenLau creates his actions and stores in his example projects.

        I had used QuickFlux once before and I don't recall having this same problem then, only now has it come to my attention.

        1 Reply Last reply
        0

        • Login

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