You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QListWidget拖放模式与单独属性设置的区别、对应属性及适用场景问询

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 InternalMove mode, 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 setting dragEnabled=True, viewport().setAcceptDrops=True, and dropIndicatorShown=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:

ModedragEnabledviewport().acceptDropsdropIndicatorShownAdditional Behavior
NoDragDropFalseFalseFalseNo drag-and-drop functionality at all.
DragOnlyTrueFalseFalseItems can be dragged out, but no drops are accepted.
DropOnlyFalseTrueTrueItems can be dropped in, but no dragging from the view is allowed.
DragDropTrueTrueTrueAllows dragging items out and accepting external drops; default action is copy (can use modifiers to trigger move).
InternalMoveTrueTrueTrueOnly 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 dropIndicatorShown manually, for example).
  • Examples:
    • Use InternalMove if you just want users to reorder items within the list without copying from other views.
    • Use DragDrop if you need to let users drag items to other views or accept items from external sources.
    • Use DragOnly or DropOnly for one-way drag-and-drop flows.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 11:55:28