Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table with Radio Button/CheckBox #4172

Closed
m21-cerutti opened this issue May 25, 2021 · 3 comments
Closed

Table with Radio Button/CheckBox #4172

m21-cerutti opened this issue May 25, 2021 · 3 comments
Labels
label/id and id stack implicit identifiers, pushid(), id stack

Comments

@m21-cerutti
Copy link

m21-cerutti commented May 25, 2021

Version/Branch of Dear ImGui:

Version: 1.83
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_opengl3.cpp + custom OpenInventor backend
Compiler: MSVC 14.16
Operating System: Windows 10

My Issue/Question:
Linked to #125.

Hello, I am trying to do a table with radio button for selectionning one item in a table.
But the problem is it don't actualise the state, and never trigger that a radio button have been clicked.
I have also tried with CheckBox and Selectionable and slight difference in code, but same result.
There is also no demo from what i have seen in this version using this particular case.

Screenshots/Video

2021_05_25_13-17-07_gui2d

radio_dearimgui

Standalone, minimal, complete and verifiable example: (see #2261)

struct ImGuiParameterState
{
  int selected_radio;
};

static ImGuiParameterState state;

if (ImGui::Begin("Control Panel"))
{
  if (ImGui::BeginTable("9x9_radio_table", 3))
  {
    int i;
    for (i = 0; i < 9; i++)
    {
      ImGui::TableNextColumn();
      if (ImGui::RadioButton("", &state.selected_radio, i))
      {
        std::cout << i << std::endl; // Never called
      }
    }
    ImGui::EndTable();
  }
  
  ImGui::End();
}
@ocornut
Copy link
Owner

ocornut commented May 25, 2021

Hello,
This is answered by the FAQ:

Q: Why is my widget not reacting when I click on it?
Q: How can I have widgets with an empty label?
Q: How can I have multiple widgets with the same label?

https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-why-is-my-widget-not-reacting-when-i-click-on-it

@ocornut ocornut closed this as completed May 25, 2021
@ocornut ocornut added the label/id and id stack implicit identifiers, pushid(), id stack label May 25, 2021
@m21-cerutti
Copy link
Author

m21-cerutti commented May 25, 2021

Oh i'm verry sorry, i have read too quickly the usage.
I think although it can be a good example, here the modified code :

if (ImGui::BeginTable("9x9_radio_table", 3))
{
  for (int i = 0; i < 9; i++)
  {
    ImGui::TableNextColumn();
    std::string id = "##radio_table_" + std::to_string(i);
    if (ImGui::RadioButton(id.c_str(), &state.selected_radio, i))
    {
      int row = ImGui::TableGetRowIndex();
      int col = ImGui::TableGetColumnIndex();
      std::cout << row << ";" << col << std::endl;
    }
  }
  ImGui::EndTable();
}

@ocornut
Copy link
Owner

ocornut commented May 25, 2021

You might want to reread this FAQ entry carefully, because in this situation using PushID(i) would be both simpler and faster.

  for (int i = 0; i < 9; i++)
  {
    ImGui::TableNextColumn();
    ImGui::PushID(i);
    if (ImGui::RadioButton("", &state.selected_radio, i))
    {
      int row = ImGui::TableGetRowIndex();
      int col = ImGui::TableGetColumnIndex();
      std::cout << row << ";" << col << std::endl;
    }
    ImGui::PopID();
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
label/id and id stack implicit identifiers, pushid(), id stack
Projects
None yet
Development

No branches or pull requests

2 participants