[PyQt] Drag & Drop with QTreeWidget

Baz Walter bazwal at ftml.net
Tue Dec 1 19:39:59 GMT 2009


> Hi all
> 
> I'm trying to implement Drag & Drop in my QTreeWidget based GUI but 
> it is not clear to me how to proceed (after having read the Doc and 
> googled it).
> 
> I have several question (tree = QTreeWidget())
>  1) I have activated:
> 	tree.setSelectionMode(QAbstractView.ExtendedSelection)
>     yet before I would just keep the mouse pressed and slide over 
> the selection I wanted but now that is not possible. Is there a way 
> to maintain such behaviour and force that the drag is only performed 
> on a second click over the selection ?

not sure i undertstand what you mean - but i would have thought the 
selection behaviour changed because you enabled drag and drop, not 
because you changed the selection mode.

>  2) how do I force a certain QTreeWidgetItem NOT to accept drops ?

http://doc.trolltech.com/4.5/qtreewidgetitem.html#setFlags

>  3) I have a specific tree:
>        RootItem
>              |
>              | ---  Campaign1
>              |            |
>              |            |---- Folder1
>              |                      |
>              |                      |------ Calc1            
> path1      (where path1= /home/ .... /Folder1/calcname1 
>              |                      |------ Calc2            
> path2      (where path2= /home/ .... /Folder1/calcname2
>              |
>              | ---  Campaign2
> 
>        So what I would like would be to drag only items of name 
> "calc*" to nodes of type "campaign" (ex: Campaign2) 

use QTreeWidgetItem flags to control which items can be dropped 
on/dragged/selected etc.

> and when doing so create the sub-node "Folder1" if it does not exist within 
> "campaign" and put the dragged items under "folder1" (and not 
> "campaign" where they were dropped). How to do this ? Do I do this 
> in a dropEvent? In a dropEventAction ?

you could monitor the tree using QTreeView.rowsInserted and then 
reparent dropped items as necessary.



More information about the PyQt mailing list