QListWidget拖放模式与单独属性设置的区别、对应属性及适用场景问询
Great question—this is a common point of confusion when working with Qt's drag-and-drop system, especially since the docs don't always spell out the full details of how setDragDropMode interacts with individual properties. Let's break this down clearly, including how it applies to Qt for Python (which behaves identically to C++ here, since it's just wrapping the same core libraries).
Are the two approaches equivalent?
No, they’re not fully equivalent—even if your example seems to produce similar results for one specific mode. The setDragDropMode method does more than just toggle individual properties like dragEnabled or acceptDrops; it also configures hidden behavior related to drop actions (move vs copy) and whether external items can be dropped into the view.
For example:
- If you use
InternalMovemode, the list will only accept drops from itself (no external items allowed) and default to moving items instead of copying. You can’t replicate this just by settingdragEnabled=True,viewport().setAcceptDrops=True, anddropIndicatorShown=True—those settings would still let you drop items from another list into this one, and the default action would be copy (unless you add custom event handling).
Which properties/attributes are actually set by the mode method?
setDragDropMode configures a bundle of related settings in one go, tailored to each mode. Here’s what each predefined mode does:
| Mode | dragEnabled | viewport().acceptDrops | dropIndicatorShown | Additional Behavior |
|---|---|---|---|---|
NoDragDrop | False | False | False | No drag-and-drop functionality at all. |
DragOnly | True | False | False | Items can be dragged out, but no drops are accepted. |
DropOnly | False | True | True | Items can be dropped in, but no dragging from the view is allowed. |
DragDrop | True | True | True | Allows dragging items out and accepting external drops; default action is copy (can use modifiers to trigger move). |
InternalMove | True | True | True | Only accepts drops from the same view; default action is move (no copy, even with modifiers). |
Beyond these visible properties, the mode also configures how the view handles drop events under the hood—like restricting external drops for InternalMove or setting the default drop action type.
What are the use cases for each one?
Use setDragDropMode when:
- You want a standard, out-of-the-box drag-and-drop behavior. This is the recommended approach for most common scenarios because it’s concise and ensures all related settings are consistent (you don’t have to remember to toggle
dropIndicatorShownmanually, for example). - Examples:
- Use
InternalMoveif you just want users to reorder items within the list without copying from other views. - Use
DragDropif you need to let users drag items to other views or accept items from external sources. - Use
DragOnlyorDropOnlyfor one-way drag-and-drop flows.
- Use
Use individual property settings when:
- You need a custom setup that doesn’t fit any of the predefined modes. For example:
- You want drag enabled and drops accepted, but don’t want to show the drop position indicator.
- You need to configure a custom drop action that’s not covered by the modes (like always moving items even when dragging to another view).
- Note: If you mix modes and individual settings, be aware that setting a mode later will override your manual property changes—so stick to one approach for a given view.
Quick note on your example
In your code, you manually set dragEnabled=True, viewport().setAcceptDrops=True, and dropIndicatorShown=True—which matches the basic properties set by DragDrop mode. But DragDrop mode also includes logic to handle external drops and default to copy actions, which your manual setup also allows (since you didn’t restrict external drops). That’s why they look equivalent here, but the mode is still the cleaner, more maintainable way to achieve this behavior.
内容来源于stack exchange

