Skip to main content

Drag and Drop

Dear ImGui provides a flexible drag and drop system that works with any widget. You can drag data between widgets, windows, and even across different Dear ImGui contexts.

Overview

The drag and drop system consists of two parts:
  • Drag Source: The widget being dragged (uses BeginDragDropSource())
  • Drop Target: The widget receiving the drop (uses BeginDragDropTarget())

Basic Workflow

Simple Drag and Drop Example

Drag and Drop Functions

BeginDragDropSource

Call after submitting an item to make it draggable:
Flags:

SetDragDropPayload

Set the data to be carried during drag:
Parameters:
  • type: User-defined string (max 32 characters, strings starting with _ are reserved)
  • data: Pointer to your data
  • size: Size of data in bytes
  • cond: When to set payload (usually 0 for always)

BeginDragDropTarget

Call after submitting an item to make it a drop target:

AcceptDragDropPayload

Accept a payload of a given type:
Returns NULL if payload type doesn’t match or if mouse button not released.

GetDragDropPayload

Peek into current payload from anywhere:
Returns NULL when drag and drop is inactive.

ImGuiPayload Structure

Dragging Between Widgets

Simple List Reordering

Dragging Custom Data

Advanced Usage

Peek Before Delivery

Preview payload without accepting it:

Custom Preview Rendering

Multiple Payload Types

Accept multiple types in one target:

Drag and Drop with Tables

Color Drag and Drop

ImGui widgets already support color drag and drop:

Best Practices

Important notes:
  • Payload data is copied and owned by Dear ImGui
  • An item can be both a drag source AND a drop target
  • If you stop calling BeginDragDropSource(), the payload is preserved but won’t show a preview
  • Type strings are limited to 32 characters

Complete Example: File Tree

Reference

  • Drag and Drop API (line 968)
  • See imgui_demo.cpp under “Widgets->Drag and Drop” for more examples