c++glfwimgui

ImGui Button doesn't recognize the click when the for loop is greater than 0?


This is the current snippet of my code:

...
class ObjectA{
    public:
        int id;
        std::string name;
        std::string description;
};
...
ObjectA a;
a.id = 1;
a.name = "Refrigerator";
a.description = "Something for cooling food";

ObjectA b;
b.id = 2;
b.name = "Candle";
b.description = "Something you light on fire with a lighter";

ObjectA c;
c.id = 3;
c.name = "Cup";
c.description = "Something you pour liquid in";

std::vector<ObjectA> obja;
obja.push_back(a);
obja.push_back(b);
obja.push_back(c);

int Display(){
    ImGui::Text("Object names:");

    for(int i=0; i<obja.size(); i++){
        ImGui::Text("%s", obja.at(i).name);
        ImGui::SameLine();
        if(ImGui::Button("More")){
            system("xman");
            ...
        }
        ...
    }
}
...
int main(){
    ...
    while(!glfwWindowShouldClose(window)){
        ...
        ImGui::Begin();
        Display();
        ImGui::End();
    }
    ...
}

The click is being recognized with the first object (obja.at(1)) but with second object nor the third.

It should also work on the other buttons but it doesn't. I already tried several things including closing this is an if(true) statement or a while(true) statement. The if(true) did nothing and the while(true) brings the application to crash. I don't know what to do anymore to make this work.


Solution

  • Got it now. The answer is to put ImGui::PushID(i); before creating the button and ImGui::PopID(); after creating the button.