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 Updated to NodeBB v4.3 + New Features

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 2.0k Views 2 Watching
  • 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 17 Jan 2018, 18:06 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
    • J Offline
      J Offline
      jpnurmi
      wrote on 17 Jan 2018, 18:35 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 17 Jan 2018, 18:58
      3
      • J jpnurmi
        17 Jan 2018, 18:35

        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 17 Jan 2018, 18:58 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
        • J Offline
          J Offline
          jpnurmi
          wrote on 17 Jan 2018, 19:09 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 17 Jan 2018, 19:30
          3
          • J jpnurmi
            17 Jan 2018, 19:09

            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 17 Jan 2018, 19:30 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
            • J Offline
              J Offline
              jpnurmi
              wrote on 17 Jan 2018, 19:53 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 17 Jan 2018, 20:29
              3
              • J jpnurmi
                17 Jan 2018, 19:53

                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 17 Jan 2018, 20:29 last edited by
                #7

                @jpnurmi Great, thanks for your help!

                1 Reply Last reply
                1

                5/7

                17 Jan 2018, 19:30

                • Login

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