Overview
This guide covers recommended patterns and practices for building maintainable, performant Dear ImGui applications.Core Principles
Immediate Mode Paradigm
Dear ImGui uses an immediate mode API where UI is rebuilt every frame:Unlike retained-mode UIs, you don’t create widgets once and modify them later. Instead, you emit them fresh each frame.
Application Owns Data
ID Management
Understanding IDs
Every interactive widget needs a unique ID. Most common mistake: using duplicate IDs.ID Stack Tool
Use the built-in tool to debug ID issues:Window Management
Always Match Begin/End
Window Flags
Use appropriate flags for your use case:Layout Best Practices
Use GetContentRegionAvail
Proper Spacing
Tables for Complex Layouts
Input Handling
Check WantCapture Flags
Style Management
Push/Pop Pattern
Initialize Style Once
Performance
Minimize Window Count
Early Exit for Collapsed Windows
Use ListClipper for Large Lists
Reduce Draw Calls
Error Prevention
Static Buffers for InputText
Static Arrays for Persistent Data
RAII Helpers
Create helpers for automatic cleanup:Organization
Modular UI Functions
Separate Data from UI
Testing and Debugging
Use Demo Window
Metrics Window
Assert on Errors
Common Pitfalls
Forgetting to Call End()
Forgetting to Call End()
Always pair Begin() with End(), even if Begin() returns false.
Duplicate IDs
Duplicate IDs
Most common beginner mistake. Use PushID or ## suffix.
Modifying Style Mid-Frame
Modifying Style Mid-Frame
Use PushStyleVar/PushStyleColor for temporary changes, not direct ImGuiStyle modification.
Not Handling UTF-8 Correctly
Not Handling UTF-8 Correctly
Use u8"" prefix for non-ASCII strings and ensure source files are UTF-8.
Assuming Fixed Layout
Assuming Fixed Layout
Dear ImGui layouts are dynamic. Use GetContentRegionAvail() instead of hardcoded sizes.
See Also
- Examples - Example applications
- Performance - Performance optimization
- Troubleshooting - Common issues
- FAQ - Frequently asked questions