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. Dynamically change a component
Forum Updated to NodeBB v4.3 + New Features

Dynamically change a component

Scheduled Pinned Locked Moved Solved QML and Qt Quick
dynamic qmlmodel-viewload
5 Posts 4 Posters 3.7k Views 1 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
    MoaMoaK
    wrote on 26 Sept 2017, 14:18 last edited by
    #1

    I have a model displayed in QML. It works with both GridView and ListView separately. I want to be able to switch between a gridView-based display and a listView-based display when I click on a button. Something like :

    Item {
        id: root
        
        GridView {
            model: m
            delegate: Text { text: "foo" }
        }
        Rectangle {
            id: button
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    /* ???????????????
                       if GridView is displayed :
                           remove GridView and add ListView
                       if ListView is displayed :
                           remove ListView and add GridView
                       ??????????????? */
            }
        }
    }
    

    I think having both GridView and ListView and setting visible property of only one to true might works but it doesn't look like a way to go to me. This is not the first time I'm facing the problem and I might endup using it in some other more complex cases so I don't want a trick that works in this specific example but more likely a generic of dynamically loading/unloading components in QML.

    R 1 Reply Last reply 26 Sept 2017, 14:27
    0
    • M MoaMoaK
      26 Sept 2017, 14:18

      I have a model displayed in QML. It works with both GridView and ListView separately. I want to be able to switch between a gridView-based display and a listView-based display when I click on a button. Something like :

      Item {
          id: root
          
          GridView {
              model: m
              delegate: Text { text: "foo" }
          }
          Rectangle {
              id: button
              MouseArea {
                  anchors.fill: parent
                  onClicked: {
                      /* ???????????????
                         if GridView is displayed :
                             remove GridView and add ListView
                         if ListView is displayed :
                             remove ListView and add GridView
                         ??????????????? */
              }
          }
      }
      

      I think having both GridView and ListView and setting visible property of only one to true might works but it doesn't look like a way to go to me. This is not the first time I'm facing the problem and I might endup using it in some other more complex cases so I don't want a trick that works in this specific example but more likely a generic of dynamically loading/unloading components in QML.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 26 Sept 2017, 14:27 last edited by raven-worx
      #2

      @MoaMoaK
      a possibility would be to use Qt.createComponent() or Qt.createQmlObject() methods (what fits better for your needs) to toggle the views. You can store the object also in a property to keep accessible later on.
      Make sure to call destroy() on the old object to remove it.

      Or the "lazy" way: Use a stack-view element, put the layouts init and assign the model to both, and just switch the index on it. This has the disadvantage that both layouts are instantiated. But the advantage that switching between them is faster. Since the object creation/destruction is obsolete.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      E 1 Reply Last reply 26 Sept 2017, 15:12
      2
      • R raven-worx
        26 Sept 2017, 14:27

        @MoaMoaK
        a possibility would be to use Qt.createComponent() or Qt.createQmlObject() methods (what fits better for your needs) to toggle the views. You can store the object also in a property to keep accessible later on.
        Make sure to call destroy() on the old object to remove it.

        Or the "lazy" way: Use a stack-view element, put the layouts init and assign the model to both, and just switch the index on it. This has the disadvantage that both layouts are instantiated. But the advantage that switching between them is faster. Since the object creation/destruction is obsolete.

        E Offline
        E Offline
        Eeli K
        wrote on 26 Sept 2017, 15:12 last edited by
        #3

        @MoaMoaK
        @raven-worx said in Dynamically change a component:

        Or the "lazy" way: Use a stack-view element

        ...or StackLayout which is simpler and meant for these kind of use cases more than StackView.

        1 Reply Last reply
        1
        • R Offline
          R Offline
          RaubTieR
          wrote on 26 Sept 2017, 17:56 last edited by RaubTieR
          #4

          May be you could simply put your GridView and ListView into the separate files and then show them in Loader? Just change its ".source" when you feel like... Worked for me.

          1 Reply Last reply
          2
          • M Offline
            M Offline
            MoaMoaK
            wrote on 27 Sept 2017, 14:31 last edited by
            #5

            Ok, nice, these three answers (StackView / StackLayout / Loader + Component) were what I was looking for. I tried each one and I think I'll stick with the StackView and the replace method as it's easy to have a fading transition with it. And I've already found a use to improve my code for the Loader so I also thank you for that.
            Thx for the help.

            1 Reply Last reply
            0

            5/5

            27 Sept 2017, 14:31

            • Login

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