Results 1 to 7 of 7

Thread: setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

  1. #1
    Join Date
    Aug 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

    I'm having problems getting my drag and drop working for a QTreeView. Everything was working fine a couple of months ago but somewhere along the line it stopped working. I've managed to get the drag and drop itself working again, but I do not get a drop indicator, and the tree doesn't expand regardless of how long I hover the drop over it.

    My tree view code looks like this:
    Qt Code:
    1. MyTreeView::MyTreeView(QWidget *parent) :
    2. QTreeView(parent)
    3. {
    4. setSelectionMode(QAbstractItemView::SingleSelection);
    5. viewport()->setAcceptDrops(true);
    6. setDragEnabled(true);
    7. setDropIndicatorShown(true);
    8. setAutoExpandDelay(0);
    9. setIndentation(16);
    10. }
    11.  
    12. void MyTreeView::dragEnterEvent(QDragEnterEvent *event)
    13. {
    14. event->acceptProposedAction();
    15. }
    16.  
    17. void MyTreeView::dragMoveEvent(QDragMoveEvent *event)
    18. {
    19. event->acceptProposedAction();
    20. }
    To copy to clipboard, switch view to plain text mode 

    I don't know if this code matters in this case, but this is my subclassed standard item model:
    Qt Code:
    1. MyItemModel::MyItemModel(QWidget *topWidget, QObject *parent) :
    2. {
    3. w = topWidget;
    4. }
    5.  
    6. Qt::DropActions MyItemModel::supportedDropActions() const
    7. {
    8. return Qt::CopyAction | Qt::MoveAction;
    9. }
    10.  
    11. Qt::ItemFlags MyItemModel::flags(const QModelIndex &index) const
    12. {
    13. QString type = index.data(FileTypeRole).toString();
    14.  
    15. // We should only be able to drop onto folders.
    16. if (type != "File" && type != "ImageFile" && index.isValid())
    17. return Qt::ItemIsDropEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
    18. else
    19. return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
    20. }
    21.  
    22. bool MyItemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
    23. {
    24. // this all works fine, so I'm cutting it for brevity.
    25. }
    26.  
    27. QStringList MyItemModel::mimeTypes () const
    28. {
    29. QStringList qstrList;
    30. // This is a list of the accepted mime types for dropping.
    31. qstrList.append("text/uri-list");
    32. return qstrList;
    33. }
    To copy to clipboard, switch view to plain text mode 

    Everything works as far and drag and drop goes, but it makes it difficult to control where you are dropping with a visual indicator and without the tree expanding for you. Any tips on how to fix this would be greatly appreciated.

  2. #2
    Join Date
    Aug 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

    I should add that everything worked until recently. I don't know what broke it, but I did upgrade to Qt 4.8 in that time frame. When it was working before, I didn't have the calls to dragEnterEvent and dragMoveEvent in there. Now, if I don't have them, drag and drop doesn't work at all. Could they be blocking access to the drag data from the model? The model accepts the drop okay, but I don't know if it sees anything that is going on before that happens. I'm not sure why it worked without those calls before and now it doesn't.

  3. #3
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

    If that is all of your tree view code, why subclass QTreeView?

    Take a look at the source code of QAbstractItemView::dragMoveEvent() (here) to see what you are overriding.

  4. #4
    Join Date
    Aug 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

    I subclassed the treeview because drag and drop doesn't work at all without the dragenterevent and dragmoveevent. My code looked exactly like the code in the item puzzle drag and drop from what I could tell, but it didn't work for me until I subclassed and added those two functions.

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

    Try forwarding the QDragMoveEvent to the base class:
    Qt Code:
    1. void MyTreeView::dragMoveEvent(QDragMoveEvent *event)
    2. {
    3. event->acceptProposedAction();
    4. QTreeView::dragMoveEvent(event);
    5. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

    That doesn't work either, drag and drop quits working completely if I do that. Drag and drop functionality comes back if I accept the action after forwarding the event, but it still doesn't autoexpand or highlight correctly.
    I really don't understand why it worked fine a month or so ago and not now. As far as I know, I haven't editing the two classes involved at all since then. And I don't think upgrading Qt would break it. It's certainly frustrating though.

  7. #7
    Join Date
    Aug 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setDropIndicatorShown and setAutoExpandDelay not working in subclassed QTreeView

    I wanted to disable auto-expansion tree nodes during darg&drop. Calling setAutoExpandDelay(-1) in constructor of subclassed tree not provide the desired result.
    I called setAutoExpandDelay(-1) in dragEnterEvent() - it helped.

Similar Threads

  1. QTreeView: expandAll() and expandToDepth(int) not working
    By papillon in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2011, 23:30
  2. Replies: 2
    Last Post: 15th February 2011, 14:10
  3. Replies: 2
    Last Post: 8th December 2010, 20:37
  4. QTreeView edit() not working.
    By billw in forum Qt Programming
    Replies: 0
    Last Post: 20th August 2010, 17:26
  5. Replies: 1
    Last Post: 28th February 2007, 08:34

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
  •  
Qt is a trademark of The Qt Company.