Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

Handle children when subclassing gtk::Container in Rust

I am trying to create a custom gtk container containing multiple children using the Rust binding for gtk. I derive my custom class from gtk::Container and gtk::Widget. The problem is as follows:

gtk::subclass::container::ContainerImpl (documentation) only provides a few methods to implement. Those methods include the add and remove methods required to make the container able to handle children. My subclass uses its own vector of widgets which is filled by the add method. Custom size negotiation and allocation is implemented and works as expected. For the most parts, I followed this tutorial (which is for plain C). But the get_children method does not work, it returns an empty vector. This is expected as the method uses the forall and foreach methods which do not know about my custom storage of widgets. But those methods are parts of gtk::ContainerExt and thus not definable by my subclass (at least without implementing all associated methods manually). The tutorial I followed overrides those methods.

I do not know how to proceed as it feels like the children not being correctly registered may lead to later memory leaks and as it renders some methods like get_children useless. Does gtk-rs have any special functionality for handling children? I am unable to find any documentation in this direction. But there must be a possibility as subclassing a container without being able to handle children is nearly useless.

question from:https://stackoverflow.com/questions/65887490/handle-children-when-subclassing-gtkcontainer-in-rust

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems like the current stable release of gtk-rs is not sufficient to implement custom containers as forall and foreach are needed but not defined in the interface. This will be fixed soon and already is in the gtk-rs git (source). If you need to implement forall (foreach will also depend on the same interface) and it still is not available in the stable release, just use the git-repo of gtk-rs as dependency source in your Cargo.toml.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...