Modify the DnDListWidget class so that when the user drops they get a pop-up menu at the mouse position with two options, Copy and Move. Modify the dragMoveEvent() to have a drop action of move rather than copy. The menu will need to go in the dropEvent(), before creating the new list item.
The QMenu.exec_() method takes a QPoint argument which tells it where to pop up; the QCursor.pos() method provides the current mouse position. The drop event's drop action must be set to copy or move depending on what the user chose.
The startDrag() method will need to be modified slightly: The start() call must be given both move and copy actions as acceptable actions, and should remove the item only if the user chose to move.
The trickiest part is deciding how to respond to the menu actions. You could use functools.partial() or lambda functions for example. In the model solution, we simply use an instance variable that holds the drop action and have two methods, one that sets the drop action to be move and the other to be copy, and simply connect the menu actions to these methods.
A subtler approach is also possible. Instead of using a pop-up menu, in the drag move event examine the keyboard modifiers and set the drop action to move by default, or to copy if the Ctrl key is pressed. Similarly, in the drop event set the drop action depending on the state of the Ctrl key. This is less intrusive than a pop-up menu, but also less obvious for casual or naive users.
In the solution, we have created two QListWidget subclasses, DnDMenuListWidget and DndCtrlListWidget, to show both of these approaches. Only about 25 lines need to be added or changed (once you have copyied/pasted DndListWidget and renamed each of the two versions) to implement both of the approaches described here.
A model solution is provided in the file chap10/customdraganddrop_ans.pyw.
This page intentionally left blank
Using Widget Style Sheets Creating Composite Widgets Subclassing Built-in Widgets Subclassing QWidget
Was this article helpful?
The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.