Re: [gtkmm] Custom types in a Gtk::TreeView



On Tue, 2004-04-06 at 13:01, Roger Leigh wrote:
> I've written a custom class, numeric, which does fast fixed-point
> arithmetic.  I'd like to display this directly in a Gtk::TreeView.
> After adding it to the TreeModel, and then adding the column to the
> view, no problems occur, but when I try to set the data, I get errors:
> 
> (test:24166): GLib-GObject-WARNING **: unable to set property
> `text' of type `gchararray' from value of type `glibmm__CustomBoxed_N4EPIC7numericE'

I guess you are using the TreeViewColumn::append_column() convenience
method, which tries to do something appropriate for you. But GTK+
doesn't know how to convert your custom type to a string (it uses a text
CellRenderer by default). I suggest that you try doing it the long way:
http://www.gtkmm.org/gtkmm2/docs/tutorial/html/ch08s02.html#id2494811

I doubt that any of the existing CellRendererText properties will be
appropriate though, as you've found, so you probably do need to either
derive a custom CellRenderer, or use the cell_data_func() callback.

You're meant to be able to put custom classes in TreeModels, so do tell
us if that doesn't work.

> What is the best way to convert from "numeric" to a "Glib::ustring".
> I've overloaded std::ostream operator<< in the class, but this doesn't
> appear to be used.

No, operator<< is not used for that.

> I can see that I can create a custom cell_data_func handler to set
> property_text, but since I will be using this in quite a lot of
> different treeviews, I don't want to have to write a handler for each
> numeric column in each view.
> 
> Would deriving a custom CellRenderer from CellRendererText be a
> possibility?  What I can't see here is what method is called to turn
> the object in the model into a Glib::ustring.  How does the
> CellRenderer "know" which TreeModelColumn it is rendering?
> 
> If I create a custom CellRenderer, is it possible to make this the
> default renderer for a particular type, so when I add a new view
> column to the treeview this renderer is chosen automatically?

You could try to add a template specialization, like the existing one. I
think it's append_column() that you need to specialize.

-- 
Murray Cumming
www.murrayc.com
murrayc murrayc com




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]