[Solved] QML and Drag and Drop
-
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"@ -
Hi Sacha, just found an issue about reparenting item. If beginResetModel()/endResetModel() called, icons will failed to be erased when moving.
Sometimes icons could be updated with new data, so I need to reset model to refresh icons. New images can be displayed out but they can't be erased anymore.
Any ideas how to fix this? Thanks in advance!
-
Yeah, if you re-parent the icons, any action on the gridView won't affect them any more (even destroying the grid view!).
So what you need to do is:- Re-parent back to gridView
- beginResetModel()/endResetModel() or other actions
- Re-parent back to loc/screen
You can think of this as two states. Its parent should be 'loc' (or screen) when it is in a moving state because you want animations. Its parent should be the gridView when it is in a modification state (reseting model/destroying gridView or whatever else you need to do).
-
Just a followup question: How can I persist the drag and dropped location so that when the user navigates away from the page and returns the arrangement that the user chose is still present.
-
I do need to keep the locations of the dragged items. What would be the best way to accomplish this?
-
Hi sfjam. Can you elaborate on your issue?
Are you scrolling to a second GridView or scrolling within the GridView?
My solution for scrolling within a GridView is on the previous page (fourth comment from bottom).
If you want multiple GridViews, the code gets more complex. Post back if this is what you desire (multi-grid draggable icon layout).
-
Wow, thanks xsacha for putting this together! It's amazing how little code is involved to get such nice complex-looking behavior. :)
I suggest a small tweak to the code: it looks like if an item at a lower index is in the active state and is moved towards an item of a higher index in the model, the active item is drawn behind the item with the higher model index. Setting the z value of the active item in the state PropertyChanges definition should do the trick:
@PropertyChanges { target: item; x: loc.mouseX - width/2; y: loc.mouseY - height/2; scale: 0.5; z: 500 }@
-
If I change line 23 in Main.qml to have "onPressed" instead of "onPressAndHold", the application is not working properly (problem with index). Where is the problem and how to solve it?
EDIT: Adding hoverEnabled: true solved this issue. But I'm not sure whether hoverEnabled has important impact on efficiency?
-
[quote author="Deqing" date="1293608551"]Cool! I solved it by simply moving "parent: loc" into the state "active".
[/quote]I am trying the same. But items are not correctly drawn (wrong x,y) if I remove "parent: loc" from Image {id: item ... }.
Can you give us your solution, please.