Results 1 to 10 of 10

Thread: Drag from tree widget

  1. #1
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Drag from tree widget

    I have subclassed QTreeWidget and want to enable drag and drop for it.

    Every thing works except one thing. When I drag elements from the tree widget, it copies the element. I would like to copy element when dragging from outside the tree widget and move element when dragging from the tree widget. I know there is a setSupportedDragActions in QAbstractItemModel but I can't use it since model() function returns a const.

    There must be a way to do this and not subclass a model right ?

    One more question, when starting a drag with Qt::CopyAction and Qt::MoveAction and dropping on a widget that accept only Qt::MoveAction, will dropMimeData be called for each drop actions specified in the startDrag ? If so, how could I know where the drag comes from ?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag from tree widget

    If so, how could I know where the drag comes from ?
    i guess u have a look at QWidget * QDrag::source (); and also target.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag from tree widget

    Quote Originally Posted by EricF View Post
    I know there is a setSupportedDragActions in QAbstractItemModel but I can't use it since model() function returns a const.
    You can use const_cast to get rid of the const modifier.
    Qt Code:
    1. QAbstractItemModel *mdl = const_cast<QAbstractItemModel*>(model());
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to wysota for this useful post:

    Micawber (27th May 2008)

  5. #4
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag from tree widget

    Yeah I know that but I just thought Trolltech made it const for a reason. And in the dropMimeData, I don't have access to QDrag object.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag from tree widget

    Sure. You shouldn't access the model using the non-model approach, but if you need to alter the drag actions, then you have no other choice. The tree class obviously misses the setSupportedDragActions method. it should be safe to use the one from the model though. I suggest you try it.

    In dropMimeData() I think you can alter the drop action if you are able to determine the source of the drag just by the contents. If not, you can reimplement dragEnterEvent in the view and do the check (and change the drag action) there although it breaks the model-view separation a bit. Unfortunately currently there is no better choice.

  7. The following user says thank you to wysota for this useful post:

    EricF (15th December 2007)

  8. #6
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag from tree widget

    Thanks that's a great point of view, I'll try that.

  9. #7
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag from tree widget

    Ok to get the fact straight, the model() function is not const, I must have been tired when I first look at the documentation. So I tried setting the supportedDracAction on it and it worked when startDrag was called but for some reason, when dragEnter, dragMove and dropEvent were called, I always got the proposed action to be Qt::CopyAction which seemed weird when I set Qt::MoveAction.

    QDrag::start functions add Qt::CopyAction no matter what suggested drop actions were. I then continued to trace and got to the platform specific implementation which I didn't want to understand. I chose Qt for a reason afterall

    So after trying some stuff, I needed to reimplement dropEvent only to set the right dropAction based on drag source because in dropMimeData, there is no way that I know of to know what is the drag source.

    So that was my solution.

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag from tree widget

    Quote Originally Posted by EricF View Post
    QDrag::start functions add Qt::CopyAction no matter what suggested drop actions were.
    I seem to remember this is a necessity on Windows.

    ... because in dropMimeData, there is no way that I know of to know what is the drag source.
    If you are the one creating the mime data, you can subclass it or provide the data you need in some other way to carry it with the object to the drop site.

  11. #9
    Join Date
    Oct 2007
    Location
    Quebec, Canada
    Posts
    40
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag from tree widget

    Quote Originally Posted by wysota View Post
    I seem to remember this is a necessity on Windows.
    This was part of QDrag implementation, QDrag::start adds Qt::CopyAction not the windows specific implementation. It's kind of funny though as this function is declared obsolete and they still use it. QDrag::exec() accepts a defaultDrop which would to the trick I think.


    Quote Originally Posted by wysota View Post
    If you are the one creating the mime data, you can subclass it or provide the data you need in some other way to carry it with the object to the drop site.
    Yeah I know because I'm already subclassing the mime data, it's just that I didn't want to add data for this specific drop target. Anyway, it works now. Thanks, again.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag from tree widget

    Quote Originally Posted by EricF View Post
    This was part of QDrag implementation, QDrag::start adds Qt::CopyAction not the windows specific implementation.
    I meant that Windows requires it (don't ask me why) and so it's implemented on all platforms to keep compatibility.

Similar Threads

  1. rich text in tree widget
    By magland in forum Qt Programming
    Replies: 19
    Last Post: 12th March 2009, 13:07
  2. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  3. Drag the Widget
    By suresh in forum Qt Programming
    Replies: 5
    Last Post: 26th May 2007, 16:06
  4. Drag Drop between Different Views
    By aamer4yu in forum Qt Programming
    Replies: 13
    Last Post: 8th December 2006, 04:29
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.