Results 1 to 7 of 7

Thread: Need help regarding drag and drop in QListView.

  1. #1
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Need help regarding drag and drop in QListView.

    Hi,

    I am working on drag and drop in QListView. For the help i am referring to Qt example puzzle in itemviews folder. Now the problem lies when the list view is in IconMode. Let me depict it:-

    Capture.PNG

    Now if i drag the item in the center of two items like shown in picture, its returning invalid index. Now while dropping i want to get the index of either left item or right. From Qt documentation what i got is :-

    "When a drop occurs, the model index corresponding to the parent item will either be valid, indicating that the drop occurred on an item, or it will be invalid, indicating that the drop occurred somewhere in the view that corresponds to top level of the model.

    int beginRow;

    if (row != -1)
    beginRow = row;
    We initially examine the row number supplied to see if we can use it to insert items into the model, regardless of whether the parent index is valid or not.

    else if (parent.isValid())
    beginRow = parent.row();
    If the parent model index is valid, the drop occurred on an item. In this simple list model, we find out the row number of the item and use that value to insert dropped items into the top level of the model.

    else
    beginRow = rowCount(QModelIndex());
    When a drop occurs elsewhere in the view, and the row number is unusable, we append items to the top level of the model."

    But this applies only for QListView in listmode.

    If i put my mouse pointer in between two items its giving invalid index. How should i handle that. Please help me in this regard.

    Thank you very much.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help regarding drag and drop in QListView.

    Quote Originally Posted by sonulohani View Post
    If i put my mouse pointer in between two items its giving invalid index. How should i handle that. Please help me in this regard.
    Isn't that already covered by your else case?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sonulohani (7th May 2016)

  4. #3
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help regarding drag and drop in QListView.

    Quote Originally Posted by anda_skoa View Post
    Isn't that already covered by your else case?

    Cheers,
    _
    Hi anda,

    Thanks for answering. I am using drop event in QListView. Any idea how to get the parent model index?

    Thank you
    Last edited by sonulohani; 7th May 2016 at 20:28.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need help regarding drag and drop in QListView.

    I am afraid I don't understand.
    Isn't your code from a list view example?

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    sonulohani (7th May 2016)

  7. #5
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help regarding drag and drop in QListView.

    Quote Originally Posted by anda_skoa View Post
    I am afraid I don't understand.
    Isn't your code from a list view example?

    Cheers,
    _
    No its not. I mean i have taken that scenario, however in my condition i am implementing dropEvent in QListView. I am sorry for making it confusing. However i am not using dropMimeData from QAbstractItemModel. I have modified the puzzle example to work with drag and drop event function instead of giving model view to handle it.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Need help regarding drag and drop in QListView.

    Your QDropEvent gives you the position of the drop. The QListView methods indexAt(), visualRect() / rectForIndex() will help with mapping that position to a QModelIndex. QListView::modelColumn() tells you which column of the model is being displayed in the view. QAbstractItemView::model() gives you the pointer to the model. QAbstractItemModel::index() with arguments 0, 0 QModelIndex() gives you the root index (parent) in the model. You can iterate over all of the QModelIndex entries in the model until you find the two (or four) that bracket the drop location.

    Go from there.

  9. The following user says thank you to d_stranz for this useful post:

    sonulohani (8th May 2016)

  10. #7
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Smile Re: Need help regarding drag and drop in QListView.

    Quote Originally Posted by d_stranz View Post
    Your QDropEvent gives you the position of the drop. The QListView methods indexAt(), visualRect() / rectForIndex() will help with mapping that position to a QModelIndex. QListView::modelColumn() tells you which column of the model is being displayed in the view. QAbstractItemView::model() gives you the pointer to the model. QAbstractItemModel::index() with arguments 0, 0 QModelIndex() gives you the root index (parent) in the model. You can iterate over all of the QModelIndex entries in the model until you find the two (or four) that bracket the drop location.

    Go from there.
    I am very very thankful to you man. You made my day. The hint you have given solved my problem. Thanks to anda_skoa also.
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

Similar Threads

  1. Problem with QListView Drag & Drop
    By QtNewbieNeedsHelp in forum Newbie
    Replies: 2
    Last Post: 22nd October 2013, 04:38
  2. qt 3.3 qlistview drag and drop
    By harishiva in forum Qt Programming
    Replies: 0
    Last Post: 21st September 2011, 13:46
  3. Replies: 2
    Last Post: 13th October 2010, 22:51
  4. Drag and Drop from QListView to QGraphicsView
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2009, 15:26
  5. qlistview drag&drop problem
    By moowy in forum Qt Programming
    Replies: 8
    Last Post: 13th October 2006, 15:49

Tags for this Thread

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.