Results 1 to 1 of 1

Thread: Race condition in qml drag&drop code? MouseArea?

  1. #1
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Race condition in qml drag&drop code? MouseArea?

    I was trying to drag an image from a QML ListView. The code below shows a horizontal list of images using a model based on QAbstractListModel. The images display fine, flicking ok.
    When I do a longpress, the image under the cursor is copied into a new QML Image and made draggable. Until the mouse button is released the ListView is disabled so it does not interfere with the dragging. Works fine...most of the time. Unfortunately from time to time the whole programm freezes. Looks like no mouse events are processed at all.

    Strangely also the
    dImage.x = mouseX
    dImage.y = mouseY
    works only most of the time. Usually the left upper edge of the image is positions directly under the mouse cursor. But from time to time the image seems to move randomly within the MouseArea.

    If a QML expert finds something strange in the code below and give me a hint.... :-)


    Qt Code:
    1. import QtQuick 1.1
    2.  
    3. Rectangle{
    4. id: topRect
    5. width: 300
    6. height: 200
    7. MouseArea{
    8. id: outerMouse
    9. anchors.fill: parent
    10. drag.filterChildren: true
    11. Rectangle{
    12. id: rect1
    13. width: topRect.width
    14. height: 100
    15. ListView {
    16. id: view1
    17. width: parent.width
    18. height: parent.height
    19. orientation: ListView.Horizontal
    20. model: myModel1
    21. delegate: Image {
    22. id: dele1
    23. width: 130; height: 100
    24. fillMode: Image.PreserveAspectFit
    25. smooth: true
    26. source: display
    27. }
    28. }
    29. ImageModel {
    30. id: myModel1
    31. mImageFolder: "./Images/"
    32. }
    33. }
    34. onPressAndHold:{
    35. if(!view1.moving){
    36. console.log("------------Start Drag--------")
    37. view1.interactive = false
    38. var idx = view1.indexAt(mouse.x+view1.contentX, mouse.y+view1.contentY)
    39. dImage.source = myModel1.indexImage(idx)
    40. drag.target = dImage
    41. dImage.x = mouseX
    42. dImage.y = mouseY
    43. }
    44. }
    45. onReleased:{
    46. view1.interactive = true
    47. dImage.source = ""
    48. console.log("released")
    49. }
    50. }
    51. Image{
    52. id: dImage
    53. width: 60
    54. height: 60
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 




    Added after 14 minutes:


    Found a solution... Though I don't understand it at all.
    To solve the random position the drag images sometimes had, I added:
    Qt Code:
    1. onPositionChanged:
    2. {
    3. dImage.x = mouseX
    4. dImage.y = mouseY
    5. }
    To copy to clipboard, switch view to plain text mode 

    to the outerMouse MouseArea. Strangely it fixed both problems. Freezing seems to be gone, too.
    Last edited by Kumosan; 11th December 2011 at 22:50.

Similar Threads

  1. Replies: 0
    Last Post: 31st October 2011, 04:01
  2. Replies: 2
    Last Post: 13th October 2010, 21:51
  3. Replies: 3
    Last Post: 10th June 2010, 15:13
  4. Replies: 0
    Last Post: 4th May 2010, 10:24
  5. Drag and Drop (drop example)
    By din9 in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 18:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.