Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reference error when accessing a QML singleton component

Reference error when accessing a QML singleton component

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 152 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.
  • S Offline
    S Offline
    shamaoke
    wrote on last edited by
    #1

    Hi.

    I'm creating a project with a singleton qml subcomponent.

    pragma Singleton
    
    import QtQuick
    
    QtObject {
    
      property int windowWidth: 1280
      property int windowHeight: 720
    
      property string windowTitle: "SamplePlayer"
    }
    

    link

    This component should then be imported in the main QML component under the Styles namespace with the Style name.

    import QtQuick
    
      ...
    
    import Styles
    
    Window {
    
      width: Style.windowWidth
      height: Style.windowHeight
    
      title: Style.windowTitle
    
      ...
    
    }
    

    link

    I defined the qml subcomponent as a qml module in the build control file.

    set_source_files_properties(Style.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
    
    ...
    
    qt_add_qml_module(
      styles
      VERSION 1.0
      URI Styles
      RESOURCE_PREFIX "/"
      PLUGIN_TARGET styles_plugin
      OUTPUT_DIRECTORY Styles
      QML_FILES
        Style.qml
      STATIC
    )
    

    link

    And linked the main executable against this module.

    target_link_libraries(
      sample_media_player
    
      PRIVATE
        ...
    
        styles_plugin
    )
    

    link

    When I build and execute the program, the style properties defined in the qml subcomponent isn't applied. I get the following error messages in the console.

    qrc:/Components/Main.qml:19: ReferenceError: Style is not defined
    qrc:/Components/Main.qml:17: ReferenceError: Style is not defined
    qrc:/Components/Main.qml:16: ReferenceError: Style is not defined
    

    What's the reason of these errors? Why the properties defined in the qml singleton component isn't applied?

    Thanks.

    Project link

    sample_media_player

    Building environments

    Arch, CMake 3.29.3, Ninja 1.12.1, GCC 14.1.1, Qt 6.7.0

    Debian 12.5, CMake 3.25, Ninja 1.11.1, GCC 12.2.0, Qt 6.4.2

    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