在 Gtk 中堆叠小部件

发布于 2024-07-25 10:04:35 字数 111 浏览 6 评论 0原文

Gtk+ 中有没有一种方法可以将一个小部件堆叠在另一个小部件之上——不包括 GtkFixed? GtkFixed 不能很好地工作有两个原因:1)我需要 Z 顺序,2)我需要一个小部件来拉伸和填充提供的空间。

Is there a way in Gtk+ to stack one widget on top of another -- not counting GtkFixed? GtkFixed doesn't work well for two reasons: 1) I need Z order, and 2) I need one widget to stretch and fill provided space.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-08-01 10:04:35

我使用 Gtk::Fixed (实际上是 gtk.Fixed - pygtk - 但我认为它在下面都是一样的)遇到了这个确切的问题,并且我能够通过操作每个小部件的窗口来轻松处理它。

就我而言,小部件已经是 EventBox 实例,我只需要确保我拖动的小部件位于顶部,否则它会滑到其他部件的下面,这看起来很错误。 解决方案非常简单,只需在单击小部件开始拖动时调用“widget.window.raise_(​​)”即可升起小部件的底层窗口。

所以我基本上只是重申之前的答案是有效的,但我想指出它实际上非常简单。 听起来您可能需要创建一些 EventBox 来保存您的小部件,但之后它应该就可以工作了。

您可以在 http://github.com/divegeek/BlockHead 中查看我正在处理的代码

I had this exact issue using a Gtk::Fixed (actually gtk.Fixed -- pygtk -- but I think it's all the same underneath) and I was able to handle it quite easily by manipulating each widget's window.

In my case, the widgets already are EventBox instances, and I just needed to make sure that the one I was dragging around was on top, because otherwise it slid underneath others, which looked quite wrong. The solution was as simple as calling "widget.window.raise_()" to raise the widget's underlying window when the widget was clicked to begin the drag.

So I'm basically just reaffirming that the previous answer works, but I wanted to point out that it's actually pretty easy. It sounds like you may need to create some EventBoxes to hold your widgets, but after that it should just work.

You can see the code I was working on at http://github.com/divegeek/BlockHead

梦魇绽荼蘼 2024-08-01 10:04:35

我认为标准 GTK 中没有合适的容器。 我会对 Gtk::Fixed 进行子类化...它仍然是您可以获得的最接近的一个,如果您使用 gtkmm,那么子类化应该不会很困难。 然后,您可以控制所有小部件的尺寸,拉伸选定的子部件以填充空间。

要控制 Z 轴,您可能需要操作小部件的 X 窗口 - 请查看有关 GDK windows² 主题的 GDK 文档。 我记得在 PyGTK 中每个小部件都有 gtk.Widget.window 属性,我想 gtkmm 也是如此。 这假设您的所有子窗口小部件都有 X 窗口,因此您需要将 Gtk::Label 包装在 Gtk::EventBox 内。

1 http://www.gtkmm.org /docs/gtkmm-2.4/docs/tutorial/html/chapter-customwidgets.html

² http://www.gtkmm.org/docs/gtkmm-2.4/docs/reference/html/classGdk_1_1Window.html#6eef65b862344ad01b01e527f2c39741

I don't think there is a proper container in standard GTK. I would subclass Gtk::Fixed... it is still the closest one you can get, and if you use gtkmm then subclassing shouldn't be very difficult¹. Then you can control the dimensions of all widgets, stretching one selected child to fill space.

To control Z-axis you will probably need to manipulate widget's X windows--check GDK documentation on topic of GDK windows². I remember that in PyGTK each widget has gtk.Widget.window property, I guess that the same is for gtkmm. This assumes that all your child widgets have X windows, so f.e. you'll need to wrap Gtk::Label inside Gtk::EventBox.

¹ http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/chapter-customwidgets.html

² http://www.gtkmm.org/docs/gtkmm-2.4/docs/reference/html/classGdk_1_1Window.html#6eef65b862344ad01b01e527f2c39741

脸赞 2024-08-01 10:04:35

我能够做到这一点 -

显示一个小部件填充整个空间,然后在顶部的固定位置显示另一个小部件,两者同时可见

,计算出合适的插入顺序(首先插入 - 底部,最后插入 - 顶部),并且 - 最重要的是!! - 强制 GtkFixed 有自己的窗口:

`f := gtk_fixed_new();
gtk_widget_set_has_window(f, 1);`

如果你不执行 set_has_window(f, 1),所有子窗口将被插入到某些父窗口小部件/窗口中(请参阅 gtk_fixed_realize()在 gtkfixed.c 中),这可能会造成 z 顺序混乱。

I was able to do exactly that -

display one widget filling the entire space, and then another on top at a fixed location, both visible at the same time

in gtk2 by using GtkFixed, working out a suitable insertion order (first inserted - bottom, last inserted - top), and - most importantly!! - forcing GtkFixed to have it's own window:

`f := gtk_fixed_new();
gtk_widget_set_has_window(f, 1);`

If you don't do set_has_window(f, 1), all children windows will be inserted into some parent widget/window (see gtk_fixed_realize() in gtkfixed.c), and that might create z-order mess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文