Re: Determining widget type

aitor_czr <[email protected]>
Newsgroups gmane.comp.gnome.gtkmm
Message-ID <[email protected]>
Hi Phil,

On 28/3/20 9:34, Phil Wolff <[email protected]> wrote:
> I want to modify the primary message widget so I can select and copy the
> text.
>
>   ??? Gtk::Box* pBox = pDialog->get_content_area ();
>   ??? std::vector<Gtk::Widget* > v = pBox->get_children ();
>   ??? Glib::ustring str = ((Gtk::Label*)v[0])->get_label ();
>
> This fails: Gtk-CRITICAL: 08:51:12.080: gtk_label_get_label: assertion
> 'GTK_IS_LABEL (label)' failed
>
> So the primary message widget isn't a Gtk::Label. How do I determine
> what it is?

Dynamic cast should do the job:

https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Keywords/dynamic_cast

You can also use the *typeinfo* library as follows:

#include <typeinfo>

std::unique_ptr<Foo> foo( new Foo() );
std::string str = typeid(foo).name();
std::cout << str << std::endl;

Even though you should filter the outputs of special characters like :: 
<> and so.
In this concrete case, the program will throw a bizarre output like this:

St10unique_ptrI4FooSt14default_deleteIS0_EE

On the other hand, if you have access to the *boost* library, you'll be 
able to get a pretty name. Read here at the end of the post:

https://stackoverflow.com/questions/351845/finding-the-type-of-an-object-in-c

But i recommend you to pay attention to the dependencies of your project.

Hope this helps,

Aitor.

_______________________________________________
gtkmm-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/gtkmm-list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.