Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-122055

QML Singleton does not get instanciated when root .qml file is in the same subfolder

    XMLWordPrintable

Details

    • Linux/X11, macOS

    Description

      Hello,
      I have a very strange case going on. Repro attached.

      Basically:

      qml/Style.qml:

      pragma Singleton
      import QtQuick
      
      QtObject
       {
          readonly property int foo: 250
          Component.onCompleted: {
              console.log("hi from singleton")
          }
      }
      

      qml/Main.qml:

      import QtQuick
      Window {
          visible: true
          Component.onCompleted: console.log(Style.foo)
      }
      

      When I do this, I get "undefined" on the console, and I don't see "hi from singleton" showing up.

      To make it work, it is sufficient to move Main.qml one folder above. e.g. here are the three scenarios:

      Scenario A: fails
      qml/Main.qml
      qml/Style.qml

      Scenario B: works
      ./Main.qml
      qml/Style.qml

      Scenario C: works
      ./Main.qml
      ./Style.qml

      I believe that if Scenario B and C work, then scenario A not working is a bug.

      Bug originally experienced on macOS but reduced and reproduced on Linux.

      CMake for the record:

      cmake_minimum_required(VERSION 3.16)
      
      project(singleton-bug VERSION 0.1 LANGUAGES CXX)
      
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      
      find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
      
      qt_standard_project_setup(REQUIRES 6.5)
      
      set_source_files_properties(qml/Style.qml PROPERTIES
          QT_QML_SINGLETON_TYPE TRUE
      )
      qt_add_executable(appsingleton-bug
          main.cpp
      )
      
      qt_add_qml_module(appsingleton-bug
          URI singleton-bug
          VERSION 1.0
          QML_FILES
              # Main.qml  # OK
              qml/Main.qml # Buggy
              qml/Style.qml
      )
      
      target_link_libraries(appsingleton-bug
          PRIVATE Qt6::Quick
      )
      
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qtqmlteam Qt Qml Team User
              jcelerier Jean-Michaël Celerier
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes