imgui: Can not open modal popup from menu / Issue with ID stack
This works:
if (ImGui::Button("Import asset"))
{
ImGui::OpenPopup("ImportAssetDialog");
}
if (ImGui::BeginPopupModal("ImportAssetDialog"))
{
ImGui::Text("test");
}
This does not:
if (ImGui::MenuItem("Import asset"))
{
ImGui::OpenPopup("ImportAssetDialog");
}
if (ImGui::BeginPopupModal("ImportAssetDialog"))
{
ImGui::Text("test");
}
This happens

About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 5
- Comments: 20 (8 by maintainers)
Commits related to this issue
- Added OpenPopup() ImGuiID overload (#3993, #331) — committed to ocornut/imgui by ocornut 3 years ago
I found a temporary solution to this bug for anyone waiting for a fix. Hopefully ocornut fixes it soon.
The exact code I use is complicated, therefore I did not paste it here. However I tried to find the simplest code to reproduce the problem, here it is:
Just to be sure I tried to place BeginPopupModal from the example in every possible place, nothing works.
The only solution I found is this, however I hope there is something better:
I’ve pulled the source code of ImGui yesterday.
Your first example can’t work anyway because the
if (ImGui::BeginPopupModal("popup"))block is only called when the menu is being browsed. If it worked it would assert anyway because you aren’t calling `EndPopup’ anywhere.The second example is correct and exactly what I have mentioned, the ID will match.
Now I think what would be desirable is to make this example work (currently it won’t because the opened popup identifier will be “mainmenubar+popup” and the begin is on “popup”, this why I was suggesting a way to alter how the popup is using - or not - the id stack.
Aha, just wanted to say that took me a while to understand why the popups didnt work after a MenuItem() as well. Got puzzled exactly like nem0 and found the same “solution”.
My solution to this is to use a lambda. A function pointer can be set at any state (even inside a menu click) and in the end of your code you simply need to call it.
Not sure if this has perfomance issues I may be overlooking though, otherwise its pretty handy:
{
}
Hi, I came up with a different work-around to this after struggling like OP has. I use PushOverrideID from the internal api:
This will work in any context 😃 hope this helps someone.
I had a similar situation trying to write a Save File Menu Item. It opens up a modal popup for you to write the full path for the file to be saved(temporary until I figure out a good way to incorporate proper File Dialogs) and that ,if the file already exists ,in turn has to open up a stacked modal popup that asks for permission to overwrite. The first modal is easy. The solution above with the bool flag toggle inside the MenuItem works fine. But that got a bit messy with bool flags too quickly due to dependencies. The first modal upon clicking Save has to either complete the action or wait for positive feedback from the second stacked modal.
Instead an enum with the states of this situation worked on the first try. Much cleaner and more straightforward.
Hello, I also encoutered this “problem”. I have a humble idea, what if when label contains for example 4# at the begining like:
ImGui::Button("####I am a global label");Then when generating ID it would just simply ignore the stack and use only inputed string for hashing. Of course that would mean that user must be more careful with them because of collisions. But it seems like relatively easy solution to me. Of course I might be wrong, because of some internal structure or something…Yes I would like to solve that but it would need to be solved in a generic manner not only for popup. I don’t know how and when yet (considering there is a workaround for popups it isn’t a top priority).