The Unholy Metal Machine
The Unholy Metal Machine

Reputation: 1103

how to test the type of the widgets returned by get_children()?

using get_children() on a Gtk::Container returns a std::vector<Gtk::Widget*>(widgets contained by the container).

a Gtk::Entry inherits from Gtk::Widget. Of course specific Gtk::Entry functions like get_text() cannot be called by a Gtk::Widget.

For solving this problem, I can cast Gtk::Widget to Gtk::Entry, but you see the problem now, how can I deal if there is some other widget in the container, let say a Gtk::Button ?

  for ( auto* widgetOfTheEvilDead : ContainerCoffin->get_children() ) 
  {
    if ( widgetOfTheEvilDead->get_visible() /*shared by all widget*/ )
    {
      // do something if i'm an entry, e.g.:
      text = static_cast<Gtk::Entry*>( widgetOfTheEvilDead )->get_text();

      if ( text == "Rotting Christ")
        this->music->play("Lucifer Over Athens");
    }

  }

to be complete, in my case I'm not using a Gtk::Entry but my own widget wich inherits from a Gtk::Entry :

class Tombstone
: public Gtk::Entry
{
.
.
.
}

Upvotes: 2

Views: 858

Answers (1)

David G
David G

Reputation: 96810

This is what dynamic_cast is for:

if (auto p = dynamic_cast<Gtk::Entry*>(widgetOfTheEvilDead)) {
    test = p->get_text();
}

Upvotes: 5

Related Questions