Search code examples
user-interfacewidgetgtkmm

How can I access on a widget at [i], stored in a Gtk::Box / Gtk::Grid?


I have a std::vector of Gtk::Boxes, that store widgets. Now, I want to access a certain widget in the Box at [i].

for(auto& it : layouts) {
        for(int i = 0; i < it->size(); ++i) {
            if(it->itemAt(i)->widget()) {
                it->itemAt(i)->widget()->setVisible((std::string(it->get_name())== StringID));
            }
        }
    }

layouts: std::vector holding Gtk::Boxes.
itemAt is a pseudo method, (QT method) to access Gtk::Box at (i).


Solution

  • Gtk::Box is not the right abstraction. Every operation on widgets is relative to other widgets present in the container. If you want control over children at a specific location in your container, I would suggest moving from Gtk::Box to Gtk::Grid, which has special methods to do what you need. For example, you could use:

    Widget* Gtk::Grid::get_child_at(int left,
                                    int top 
                                   )    
    

    See the reference for more information.