GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Open and closing tree nodes · Issue #6754 · ocornut/imgui

Open and closing tree nodes #6754

New issue
Copy link
New issue
Copy link
Closed
Closed
Open and closing tree nodes#6754
Copy link
Labels
treetree nodes

Description

@talkalotwhenim
talkalotwhenim
opened on Aug 27, 2023
Issue body actions

I followed the portion of the demo that uses buttons to open and close all tree nodes. Instead of two buttons, I wanted to control this with a menu item which works fine. The problem I have is if I try to open and close individual tree nodes like usual, they do not stay open or closed.

Standalone, minimal, complete and verifiable example:

int open_action = -1;
static bool open = false;

// Loop
if (ImGui::MenuItem("Open all", nullptr, &open))
   open_action = 1;
else
   open_action = 0;

if (open_action != -1)
   ImGui::SetNextItemOpen(open_action != 0);

// Make tree node

Activity

  1. PathogenDavid commented on Aug 27, 2023

    @PathogenDavid
    PathogenDavid
    on Aug 27, 2023
    Contributor
    More actions

    ImGui::MenuItem returns true when it is clicked and false otherwise, so you're constantly setting open_action to 0 except on the frames where the menu item is clicked. (Which effectively means the tree nodes will be collapsed on every frame.)

  2. talkalotwhenim commented on Aug 27, 2023

    @talkalotwhenim
    talkalotwhenim
    on Aug 27, 2023
    Author
    More actions

    So is it possible to achieve this with a single widget or would I need two?

  3. GamingMinds-DanielC commented on Aug 27, 2023

    @GamingMinds-DanielC
    GamingMinds-DanielC
    on Aug 27, 2023
    Contributor
    More actions

    Instead of the snippet above, try this:

    static bool open = false;
    
    // Loop
    if (ImGui::MenuItem("Open all", nullptr, &open))
       ImGui::SetNextItemOpen(open);
    
    // Make tree node

    The important part is to call SetNextItemOpen only when you want to expand or collapse, not every frame.

    Edit: well, you can call it every frame if you want to, but then you need to supply if you want the item expanded, not a toggle of the status as you did before. The "every frame version" would look as simple as this:

    static bool open = false;
    
    // Loop
    ImGui::MenuItem("Open all", nullptr, &open);
    ImGui::SetNextItemOpen(open);
    
    // Make tree node
  4. ocornut
    added
    treetree nodes
    on Aug 27, 2023
  5. talkalotwhenim commented on Aug 27, 2023

    @talkalotwhenim
    talkalotwhenim
    on Aug 27, 2023
    Author
    More actions

    Unfortunately none of these worked for me

  6. PathogenDavid commented on Aug 28, 2023

    @PathogenDavid
    PathogenDavid
    on Aug 28, 2023
    Contributor
    More actions

    So is it possible to achieve this with a single widget or would I need two?

    It really depends on the exact semantics you want out of this user interaction. My first instinct was to reply with something very similar to what Daniel recommended, so clearly there's a disconnect between our guesses and what you're envisioning.

    It would be helpful to know what your actual UI looks like and what problem you're trying to solve rather than trying to apply seemingly arbitrary changes to the Dear ImGui demo.

  7. GamingMinds-DanielC commented on Aug 28, 2023

    @GamingMinds-DanielC
    GamingMinds-DanielC
    on Aug 28, 2023
    Contributor
    More actions

    Unfortunately none of these worked for me

    Both versions work when f.e. followed with a non-leaf ImGui::TreeNodeEx. So my best guess is a mistake while transporting these solutions to actual code with a different structure. As David says, seeing that code would help.

  8. ocornut commented on Jul 3, 2024

    @ocornut
    ocornut
    on Jul 3, 2024
    Owner
    More actions

    Closing as lacking feedback from OP. Don't hesitate to post here again if you need help, but right now we lacked details.

  9. ocornut
    closed this as completedon Jul 3, 2024
  10. ocornut
    added a commit that references this issue on Feb 23, 2026

    TreeNode: moved TreeNodeGetOpen() helper to public API. (#3823, #9251, …

    f6e0953
  11. brentfpage
    added a commit that references this issue on Feb 24, 2026

    TreeNode: moved TreeNodeGetOpen() helper to public API. (ocornut#3823, …

    192667c
  12. ocornut
    added a commit that references this issue on Feb 24, 2026

    TreeNode: fixed duplicate TreeNodeGetOpen() declaration. (#3823, #9251,

    0666da6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    treetree nodes

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.