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. [Solved] QML and Drag and Drop

[Solved] QML and Drag and Drop

Scheduled Pinned Locked Moved QML and Qt Quick
42 Posts 8 Posters 38.3k 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.
  • D Offline
    D Offline
    Deqing
    wrote on last edited by
    #9

    Hi Sacha,

    The demo of drag and drop code looks cool!

    I tried the code(Main2.qml) of second version in the wiki but failed with following error: "ReferenceError: Can't find variable: gridId"

    Is there anything I can do to avoid this?

    THanks

    1 Reply Last reply
    0
    • X Offline
      X Offline
      xsacha
      wrote on last edited by
      #10

      Well, I didn't write the second version.
      I tested my (first) version and provided a video of that exact code running.

      I can review the second one for you to discover the issue. It seems a lot better than my version so I hope you can get it working.

      • Sacha
      1 Reply Last reply
      0
      • B Offline
        B Offline
        baysmith
        wrote on last edited by
        #11

        I'm responsible for the second version. I tried to update this post with a "I’ve updated the above wiki entry with an alternate implementation." like I did on the "other thread":http://developer.qt.nokia.com/forums/viewreply/14354/, but it failed for some reason.

        As it turns out, the second version requires Qt 4.7.1. It gives that error with Qt 4.7.0. Thanks for pointing it out. I wasn't aware of this difference in the versions. I've updated the wiki page with a notation about this.

        Nokia Certified Qt Specialist.

        1 Reply Last reply
        0
        • X Offline
          X Offline
          xsacha
          wrote on last edited by
          #12

          By the way, I just fixed in a critical issue with the first version in the wiki. It was to do with the moving code.
          Problem was discovered and fixed in this thread: http://developer.qt.nokia.com/forums/viewthread/2460/P15/

          The issue was that if you dragged items forward, the call to 'move' would push the indices backwards (to -1, -2 and so on) and you would no longer be able to drag the '-1' index and other items would become out of whack.

          It is most noticeable on a 3-item grid.

          I think the issue is only if you don't have animation onMove. The second example uses such an animation so it should be fine. Just be careful if you disable the animation.

          • Sacha
          1 Reply Last reply
          0
          • D Offline
            D Offline
            Deqing
            wrote on last edited by
            #13

            Thank you guys. I finally get the second version working.

            It turns out that "gridId" should be defined in WidgetModel.qml, and it must starts with 0. For example:

            @
            ListElement { icon: "images/widget1.png"; gridId: 0 }
            ListElement { icon: "images/widget2.png"; gridId: 1 }
            ListElement { icon: "images/widget3.png"; gridId: 2 }
            @

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kyleplattner
              wrote on last edited by
              #14

              Still returning:

              @file:///Users/kp/Desktop/Precision Work/Screen Design/FinalScreens/QMLFinal/Rearrange4.qml:22: TypeError: Result of expression 'grid.items' [undefined] is not an object.@

              1 Reply Last reply
              0
              • X Offline
                X Offline
                xsacha
                wrote on last edited by
                #15

                I have made a cleaner version of the 'second version'
                Halved the lines of code, removed quite a lot of redundancy and doesn't require Qt4.7.1.
                Personally, I think the grids list is a bit silly when it can be done with the model/delegate. So I have completely removed the IconItem and grids.
                No loss of functionality.
                I replaced the first version with it. You can check it out in the wiki.

                • Sacha
                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  baysmith
                  wrote on last edited by
                  #16

                  The grids list was made because when I added behaviors in the delegate, it wasn't working. I don't know why it wasn't working for me, but what you've got is working. I'll remove the second version now that it is obsolete.

                  Nokia Certified Qt Specialist.

                  1 Reply Last reply
                  0
                  • X Offline
                    X Offline
                    xsacha
                    wrote on last edited by
                    #17

                    Yeah, I know, you can't animate a GridList.
                    I discussed this "in another thread.":http://developer.qt.nokia.com/forums/viewthread/2327/#13163

                    Basically, you need to reparent the items within the delegate. Then you have complete freedom of movement and animation. I reparented the images to the MouseArea.

                    • Sacha
                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Deqing
                      wrote on last edited by
                      #18

                      Looks like in the new code the grid can't be resized, it always be 3*3.

                      For example if I drag the size of its parent to a long list, it should be automatically rearranged to a 1*9 grid, as the GridView will do.

                      1 Reply Last reply
                      0
                      • X Offline
                        X Offline
                        xsacha
                        wrote on last edited by
                        #19

                        When you drag the window?
                        Oh I had removed that as I was running it on a mobile device. I'll add that back in, thanks.

                        • Sacha
                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Deqing
                          wrote on last edited by
                          #20

                          Just get it works by simply set width and height of GridView equal to its parent. :)

                          1 Reply Last reply
                          0
                          • X Offline
                            X Offline
                            xsacha
                            wrote on last edited by
                            #21

                            Yeah it already does that.
                            Anchor is fill to parent and parent width is whatever the host operating system gives it. 640x480 is just for reference.
                            Try it out now?
                            I think the qmlviewer on Windows won't allow you to resize the window smaller than 640x480 by the way. Might need a custom qmlviewer for that.

                            • Sacha
                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              Deqing
                              wrote on last edited by
                              #22

                              As I can see, the size of qmlViewer will be the same as the top item in the qml file when it starts.

                              Now I'm thinking of storing position of these icons.

                              I guess the position should be stored in the model(or somewhere else?). How to show icons in GridView according to the values stored in the model?

                              1 Reply Last reply
                              0
                              • X Offline
                                X Offline
                                xsacha
                                wrote on last edited by
                                #23

                                Well you actually cannot change the positions of items inside a GridView.

                                What I have done is created items within the GridView and then reparented them to a MouseArea (outside of GridView) so that I can move them freely.
                                Then I assign the x,y values of the GridView locations.

                                If you were to create entirely new locations and not use a GridView at all, you may as well use a Flow element.

                                If you're just using GridView/ListView for the model/delegate, then I will answer your question:

                                In your model, create an 'x' and 'y' property. For example 'myx', 'myy'. Then in the image, just set:
                                @x=myx; y=my@

                                • Sacha
                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  Deqing
                                  wrote on last edited by
                                  #24

                                  Basically I will have a serialized icons provided by C++ code via a model, and showed out as a Gridview.

                                  These icons then can be rearranged by user, and when user reopen the program they should be seeing the same order as they set last time.

                                  I'm really new to QML and not sure which is the best way, Flow or GridView?

                                  1 Reply Last reply
                                  0
                                  • D Offline
                                    D Offline
                                    Deqing
                                    wrote on last edited by
                                    #25

                                    Had tried setting x,y in model for image to display, but looks like it is not working.

                                    I guess the reason is in this case Grid index is not matching correct image, say a 2*3 grid:

                                    ID of GridView
                                    0 1 2
                                    3 4 5

                                    Images displayed in self-defined position
                                    4 5 1
                                    0 2 3

                                    When clicking at the top-left item, we'd like to pick image 4, but grid.at(index) will return 0 for us.

                                    1 Reply Last reply
                                    0
                                    • X Offline
                                      X Offline
                                      xsacha
                                      wrote on last edited by
                                      #26

                                      Oh, I see what you mean.

                                      Well the code I have in the wiki does exactly this.

                                      All you need to do is save the model in to either:

                                      1. QML file (overwrite the existing one on the fly)
                                      2. QSettings file (do the model positions inside QSettings object).

                                      The gridIDs are irrelevant. You can use gridId=843423 if you want. It's just a reference point.
                                      The only thing that matters is the order of the ListElements inside the ListModel.

                                      However, in saying this, if you keep the gridIDs as is and save the final gridIDs in to a QSettings object (uses less space/quicker to load), then you can simply use 'move' commands at runtime to sort them in to the correct order. It's another option.

                                      Since you said you are loading the model via a C++ backend, it only makes sense to load the model in the order you want. This solves your problem.

                                      • Sacha
                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        Deqing
                                        wrote on last edited by
                                        #27

                                        Thanks. I will try it in my C++ code.

                                        Another concern is about flickable. Sometimes user would like to flick the page when they have a lot of icons that displayed out of the screen. GridView have this feature by setting 'interactive' to true. However if we reparent items out of the GridView, these items will lost this feature.

                                        Actually I tried to comment out reparenting code, IconItem.qml like this:
                                        @
                                        Component {
                                        // Rectangle {
                                        // id: main
                                        // width: grid.cellWidth; height: grid.cellHeight
                                        // opacity: 0.5
                                        Image {
                                        id: item//; parent: loc
                                        width: grid.cellWidth; height: grid.cellHeight
                                        @

                                        In this code icon can be rearranged inside the GridView somehow, but grid failed to find correct index underneath cursor(don't know why)

                                        Is it possible to add flickable back to the GridView?

                                        1 Reply Last reply
                                        0
                                        • X Offline
                                          X Offline
                                          xsacha
                                          wrote on last edited by
                                          #28

                                          I only set interactive to false because it makes it easier to demo and you won't accidently flick the grid. It was unnecessary with only a 3x3 grid.

                                          If you want to set interactive to true, just remember to use contentX and/or (depending on flickable direction) contentY to get the position within the flickable (GridView) item.

                                          Example:
                                          @y: main.y - grid.contentY + 5@

                                          Note: You'll also have to make sure the grid doesn't flick while you are moving an icon.
                                          @interactive: loc.currentId == -1@

                                          With these two lines, it is working perfect with flickable here.

                                          Edit: You may get some extraordinary lagg and weird effects with flickable. You need to modify Behavior on x and y, with this:
                                          @ enabled: loc.currentId != -1 && item.state != "active"@

                                          • Sacha
                                          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