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. qt_add_qml_module + singleton confusion

qt_add_qml_module + singleton confusion

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 885 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.
  • R Offline
    R Offline
    RobertB
    wrote on last edited by RobertB
    #1

    TL;DR:

    1. Do I need qmldir or will CMake generate it?
    2. Do I need RESOURCE_PREFIX / in qt_add_qml_module() ?
    3. Do I need to link against something after calling qt_add_qml_module() ?
    4. When do I call qt_add_qml_module() ? Before or after qt_add_resources() ? Before or after qt_add_executable() ?
    5. What is the correct way of importing?
      • import "FontAwesome"
      • import FontAwesome
      • import FontAwesome 1.0

    The issue

    I have a singleton QML file I am trying to add, it looks like:

    pragma Singleton
    import QtQuick
    
    Object {
        FontLoader {
            id: regular
            source: "./fa-regular-400.otf"
        }
    
        property string fontFamily: regular.name
        property string addressBook : "\uf2b9"
    }
    

    So that I can do:

    import FontAwesome
    
    Text {
        text: FontAwesome.addressBook
    }
    

    I checked the documentation on qt_add_qml_module but unfortunately it seems to be written by a programmer, thus incomprehensible to regular humans.

    I tried:

    set_source_files_properties(FontAwesome.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
    
    qt_add_qml_module(FontAwesome
        URI FontAwesome
        VERSION 1.0
        QML_FILES
          FontAwesome.qml
    )
    

    But when running the application I get:

    [     0.268 warning] test default unknown - qrc:/app/qml/Main.qml:11:1: module "FontAwesome" is not installed 
         import FontAwesome 
         ^
    

    Note: I can specify a filename that does not exist and it continues the configure step regardless. So I don't know if qt_add_qml_module() actually does anything...

    Lets try to link against it because why not:

    target_link_libraries(test PRIVATE
      Qt6::Core
      Qt6::Gui
    [...]
      FontAwesomeplugin
    )
    

    But that yields in:

    CMake Error at src/CMakeLists.txt:34 (target_link_libraries):
      Target "FontAwesomeplugin" of type MODULE_LIBRARY may not be linked into
      another target.  One may link only to INTERFACE, OBJECT, STATIC or SHARED
      libraries, or to executables with the ENABLE_EXPORTS property set.
    

    How to proceed? Anyone have some tips?

    MesrineM P 3 Replies Last reply
    0
    • P Offline
      P Offline
      petero3
      wrote on last edited by
      #2

      Any luck?
      Am having similar issues.
      Am following this
      https://wiki.qt.io/Qml_Styling#Approach_2:_Style_Singleton
      and this
      https://doc.qt.io/qt-6/qtqml-modules-qmldir.html#object-type-declaration
      but those still leave the reader hanging re:
      the CMake side and
      if/why so much boilerplate is really needed (in the .qml and qmldir and CMake) just to make one style property accessible from other qml files.
      Thank you.

      P 1 Reply Last reply
      0
      • P petero3

        Any luck?
        Am having similar issues.
        Am following this
        https://wiki.qt.io/Qml_Styling#Approach_2:_Style_Singleton
        and this
        https://doc.qt.io/qt-6/qtqml-modules-qmldir.html#object-type-declaration
        but those still leave the reader hanging re:
        the CMake side and
        if/why so much boilerplate is really needed (in the .qml and qmldir and CMake) just to make one style property accessible from other qml files.
        Thank you.

        P Offline
        P Offline
        petero3
        wrote on last edited by
        #3

        And
        https://www.qt.io/blog/qml-modules-in-qt-6.2

        1 Reply Last reply
        0
        • R RobertB

          TL;DR:

          1. Do I need qmldir or will CMake generate it?
          2. Do I need RESOURCE_PREFIX / in qt_add_qml_module() ?
          3. Do I need to link against something after calling qt_add_qml_module() ?
          4. When do I call qt_add_qml_module() ? Before or after qt_add_resources() ? Before or after qt_add_executable() ?
          5. What is the correct way of importing?
            • import "FontAwesome"
            • import FontAwesome
            • import FontAwesome 1.0

          The issue

          I have a singleton QML file I am trying to add, it looks like:

          pragma Singleton
          import QtQuick
          
          Object {
              FontLoader {
                  id: regular
                  source: "./fa-regular-400.otf"
              }
          
              property string fontFamily: regular.name
              property string addressBook : "\uf2b9"
          }
          

          So that I can do:

          import FontAwesome
          
          Text {
              text: FontAwesome.addressBook
          }
          

          I checked the documentation on qt_add_qml_module but unfortunately it seems to be written by a programmer, thus incomprehensible to regular humans.

          I tried:

          set_source_files_properties(FontAwesome.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
          
          qt_add_qml_module(FontAwesome
              URI FontAwesome
              VERSION 1.0
              QML_FILES
                FontAwesome.qml
          )
          

          But when running the application I get:

          [     0.268 warning] test default unknown - qrc:/app/qml/Main.qml:11:1: module "FontAwesome" is not installed 
               import FontAwesome 
               ^
          

          Note: I can specify a filename that does not exist and it continues the configure step regardless. So I don't know if qt_add_qml_module() actually does anything...

          Lets try to link against it because why not:

          target_link_libraries(test PRIVATE
            Qt6::Core
            Qt6::Gui
          [...]
            FontAwesomeplugin
          )
          

          But that yields in:

          CMake Error at src/CMakeLists.txt:34 (target_link_libraries):
            Target "FontAwesomeplugin" of type MODULE_LIBRARY may not be linked into
            another target.  One may link only to INTERFACE, OBJECT, STATIC or SHARED
            libraries, or to executables with the ENABLE_EXPORTS property set.
          

          How to proceed? Anyone have some tips?

          MesrineM Offline
          MesrineM Offline
          Mesrine
          wrote on last edited by
          #4

          @RobertB said in qt_add_qml_module + singleton confusion:

          FontAwesome

          I believe what you are doing is mostly correct.
          It would help if you linked to the FontAwesome target.
          You link to the FontAwesomeplugin target if the FontAwsome library is static.
          Sometimes the engine would not find your module FontAwsome
          when is compiled as a shared library for that the module has to be loaded at runtime by setting the QML_IMPORT_PATH environment variable or as explained here.

          To be safer try to not use the same name for the Singleton type and the module.

          https://forum.qt.io/post/762513

          1 Reply Last reply
          0
          • R RobertB

            TL;DR:

            1. Do I need qmldir or will CMake generate it?
            2. Do I need RESOURCE_PREFIX / in qt_add_qml_module() ?
            3. Do I need to link against something after calling qt_add_qml_module() ?
            4. When do I call qt_add_qml_module() ? Before or after qt_add_resources() ? Before or after qt_add_executable() ?
            5. What is the correct way of importing?
              • import "FontAwesome"
              • import FontAwesome
              • import FontAwesome 1.0

            The issue

            I have a singleton QML file I am trying to add, it looks like:

            pragma Singleton
            import QtQuick
            
            Object {
                FontLoader {
                    id: regular
                    source: "./fa-regular-400.otf"
                }
            
                property string fontFamily: regular.name
                property string addressBook : "\uf2b9"
            }
            

            So that I can do:

            import FontAwesome
            
            Text {
                text: FontAwesome.addressBook
            }
            

            I checked the documentation on qt_add_qml_module but unfortunately it seems to be written by a programmer, thus incomprehensible to regular humans.

            I tried:

            set_source_files_properties(FontAwesome.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
            
            qt_add_qml_module(FontAwesome
                URI FontAwesome
                VERSION 1.0
                QML_FILES
                  FontAwesome.qml
            )
            

            But when running the application I get:

            [     0.268 warning] test default unknown - qrc:/app/qml/Main.qml:11:1: module "FontAwesome" is not installed 
                 import FontAwesome 
                 ^
            

            Note: I can specify a filename that does not exist and it continues the configure step regardless. So I don't know if qt_add_qml_module() actually does anything...

            Lets try to link against it because why not:

            target_link_libraries(test PRIVATE
              Qt6::Core
              Qt6::Gui
            [...]
              FontAwesomeplugin
            )
            

            But that yields in:

            CMake Error at src/CMakeLists.txt:34 (target_link_libraries):
              Target "FontAwesomeplugin" of type MODULE_LIBRARY may not be linked into
              another target.  One may link only to INTERFACE, OBJECT, STATIC or SHARED
              libraries, or to executables with the ENABLE_EXPORTS property set.
            

            How to proceed? Anyone have some tips?

            P Offline
            P Offline
            petero3
            wrote on last edited by petero3
            #5

            The following works for me when running the app, however when trying to preview it under the Design view in Qt Creator, it doesn't display the expected color.

            // CMakeLists.txt
            ...
            qt_add_qml_module(appsingleton
            URI singleton
            VERSION 1.0
            QML_FILES Main.qml
            QML_FILES Shapes.qml
            )

            qt_add_qml_module(CustomStyles
            URI CustomStyles
            VERSION 1.0
            QML_FILES CustomStyles/Style.qml
            RESOURCES CustomStyles/qmldir
            )
            ..

            // CustomStyles/Style.qml
            pragma Singleton
            import QtQuick 2.15

            QtObject {
            property color color: "green"
            }

            // CustomStyles/qmldir
            module CustomStyles
            singleton Style 1.0 Style.qml

            // Shapes.qml
            import CustomStyles
            ...
            color: Style.color

            P 1 Reply Last reply
            0
            • P petero3

              The following works for me when running the app, however when trying to preview it under the Design view in Qt Creator, it doesn't display the expected color.

              // CMakeLists.txt
              ...
              qt_add_qml_module(appsingleton
              URI singleton
              VERSION 1.0
              QML_FILES Main.qml
              QML_FILES Shapes.qml
              )

              qt_add_qml_module(CustomStyles
              URI CustomStyles
              VERSION 1.0
              QML_FILES CustomStyles/Style.qml
              RESOURCES CustomStyles/qmldir
              )
              ..

              // CustomStyles/Style.qml
              pragma Singleton
              import QtQuick 2.15

              QtObject {
              property color color: "green"
              }

              // CustomStyles/qmldir
              module CustomStyles
              singleton Style 1.0 Style.qml

              // Shapes.qml
              import CustomStyles
              ...
              color: Style.color

              P Offline
              P Offline
              petero3
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • R RobertB

                TL;DR:

                1. Do I need qmldir or will CMake generate it?
                2. Do I need RESOURCE_PREFIX / in qt_add_qml_module() ?
                3. Do I need to link against something after calling qt_add_qml_module() ?
                4. When do I call qt_add_qml_module() ? Before or after qt_add_resources() ? Before or after qt_add_executable() ?
                5. What is the correct way of importing?
                  • import "FontAwesome"
                  • import FontAwesome
                  • import FontAwesome 1.0

                The issue

                I have a singleton QML file I am trying to add, it looks like:

                pragma Singleton
                import QtQuick
                
                Object {
                    FontLoader {
                        id: regular
                        source: "./fa-regular-400.otf"
                    }
                
                    property string fontFamily: regular.name
                    property string addressBook : "\uf2b9"
                }
                

                So that I can do:

                import FontAwesome
                
                Text {
                    text: FontAwesome.addressBook
                }
                

                I checked the documentation on qt_add_qml_module but unfortunately it seems to be written by a programmer, thus incomprehensible to regular humans.

                I tried:

                set_source_files_properties(FontAwesome.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                
                qt_add_qml_module(FontAwesome
                    URI FontAwesome
                    VERSION 1.0
                    QML_FILES
                      FontAwesome.qml
                )
                

                But when running the application I get:

                [     0.268 warning] test default unknown - qrc:/app/qml/Main.qml:11:1: module "FontAwesome" is not installed 
                     import FontAwesome 
                     ^
                

                Note: I can specify a filename that does not exist and it continues the configure step regardless. So I don't know if qt_add_qml_module() actually does anything...

                Lets try to link against it because why not:

                target_link_libraries(test PRIVATE
                  Qt6::Core
                  Qt6::Gui
                [...]
                  FontAwesomeplugin
                )
                

                But that yields in:

                CMake Error at src/CMakeLists.txt:34 (target_link_libraries):
                  Target "FontAwesomeplugin" of type MODULE_LIBRARY may not be linked into
                  another target.  One may link only to INTERFACE, OBJECT, STATIC or SHARED
                  libraries, or to executables with the ENABLE_EXPORTS property set.
                

                How to proceed? Anyone have some tips?

                P Offline
                P Offline
                petero3
                wrote on last edited by
                #7

                @RobertB
                If you do
                File | New Project | Qt Quick Application
                x Creates a project that you can open in Qt Design Studio
                that gives you a "working" starter project which uses a singleton Constants.qml
                Well it works running, but also doesn't seem to work in Design mode of Qt Creator?

                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