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. Help to understand the necessity of this "import" statement in modified flatstyle example
Forum Update on Monday, May 27th 2025

Help to understand the necessity of this "import" statement in modified flatstyle example

Scheduled Pinned Locked Moved Solved QML and Qt Quick
flatstyleimportqmlstyles
7 Posts 2 Posters 1.9k 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.
  • M Offline
    M Offline
    markugra
    wrote on last edited by
    #1

    Hi there,
    I am new to QML and I tried to create a custom style and custom components based on the flatstyle example:

    https://doc.qt.io/qt-5.10/qtquickcontrols2-flatstyle-example.html

    Running this example shows that the custom components in the "Flat" directory (e.g. Flat/Button.qml) are being used. So far so good. However, if I rename the Button component in the MainForm.ui.qml to TheButton and rename the corresponding qml-file in the Flat directory accordingly (i.e. Button.qml -> TheButton.qml), I get the error "TheButton is not a type" when running the application.
    This can be avoided by adding an import "Flat" to the MainForm.ui.qml, but I am wondering why this is necessary (only after renaming)?

    Can anyone enlighten me or give me some pointers to relevant documentation?
    Thanks in advance.

    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      The example specifies the Flat directory as a Qt Quick Controls 2 style in the :/qtquickcontrols2.conf configuration file:

      [Controls]
      Style=Flat
      

      That way, Qt Quick Controls 2 literally registers Flat/Button.qml as the Button type, so it is available via the QtQuick.Controls 2.x import. See Creating a Custom Style for more details.

      M 1 Reply Last reply
      3
      • jpnurmiJ jpnurmi

        The example specifies the Flat directory as a Qt Quick Controls 2 style in the :/qtquickcontrols2.conf configuration file:

        [Controls]
        Style=Flat
        

        That way, Qt Quick Controls 2 literally registers Flat/Button.qml as the Button type, so it is available via the QtQuick.Controls 2.x import. See Creating a Custom Style for more details.

        M Offline
        M Offline
        markugra
        wrote on last edited by
        #3

        @jpnurmi Thanks for your reply. So if I understand correctly a new component (TheButton) would have to be defined separately somewhere else and in the Flat directory I could redefine it for the Flat style? Or can I only apply the custom style to components defined in QtQuick.Controls 2.x?

        1 Reply Last reply
        0
        • jpnurmiJ Offline
          jpnurmiJ Offline
          jpnurmi
          wrote on last edited by
          #4

          You can create a new TheButton QML type (TheButton.qml) basically anywhere you want, but for the sake of clarity, you might not want to mix it with the Qt Quick Controls 2 style.

          For your own custom types, such as TheButton, normal QML import rules apply. If TheButton.qml is in the same folder, no import is needed at all. For example, with the following structure, main.qml can create an instance of TheButton without having to import anything.

          myapp/
          |___ main.qml
          |___ TheButton.qml
          

          However, if TheButton.qml is in a different folder, the folder needs to be imported. For example, if the structure was as follows, main.qml would have to import "TheControls" to be able to create an instance of TheButton.

          myapp/
          |___ main.qml
          |___ TheControls/
               |___ TheButton.qml
          
          M 1 Reply Last reply
          3
          • jpnurmiJ jpnurmi

            You can create a new TheButton QML type (TheButton.qml) basically anywhere you want, but for the sake of clarity, you might not want to mix it with the Qt Quick Controls 2 style.

            For your own custom types, such as TheButton, normal QML import rules apply. If TheButton.qml is in the same folder, no import is needed at all. For example, with the following structure, main.qml can create an instance of TheButton without having to import anything.

            myapp/
            |___ main.qml
            |___ TheButton.qml
            

            However, if TheButton.qml is in a different folder, the folder needs to be imported. For example, if the structure was as follows, main.qml would have to import "TheControls" to be able to create an instance of TheButton.

            myapp/
            |___ main.qml
            |___ TheControls/
                 |___ TheButton.qml
            
            M Offline
            M Offline
            markugra
            wrote on last edited by
            #5

            @jpnurmi Ok, and if I have defined TheButton somewhere and imported it correctly, could I then create another THeButton.qml in the Flat folder to define its looks in the Flat style or is that only possible for default controls? Thanks a lot!

            1 Reply Last reply
            0
            • jpnurmiJ Offline
              jpnurmiJ Offline
              jpnurmi
              wrote on last edited by
              #6

              The styling system in Qt Quick Controls 2 looks up and registers only known types. However, the styling system is based on file selectors, which you can use to build/select your own styled types.

              M 1 Reply Last reply
              3
              • jpnurmiJ jpnurmi

                The styling system in Qt Quick Controls 2 looks up and registers only known types. However, the styling system is based on file selectors, which you can use to build/select your own styled types.

                M Offline
                M Offline
                markugra
                wrote on last edited by
                #7

                @jpnurmi Great, thanks for your help!

                1 Reply Last reply
                1

                • Login

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