如何向 GtkMM 中的自定义 Widget 添加 ScrolledWindow 支持?
我正在为 Gtkmm 编写一个自定义小部件,它应该显示一个巨大的数据集(想象一下类似 4096x256 字符数据表的东西)。
主要是出于优雅的原因,但也是为了在 Glade/Gtk-Builder 编辑器中可能使用,我希望这个小部件原生支持 ScrolledWindow,也就是说,一旦将其设置为 ScrolledWindow 的子项,它就会被识别为可滚动小部件,允许在其上设置水平和垂直调整对象,随后可以对其进行篡改。
似乎是这样做的,我需要在我的小部件的构造函数中执行类似的操作:
// get Gtk C type from wrapper class
GtkWidget* gwidget = this->gobj();
// imagine code here that magically creates a gobject signal,
// that we can catch in C++.
// this is actually the part which I don't know how to do.
guint my_signal = magic_way_to_create_this_signal(
&MyClass::rainbow_unicorn_signal_handler);
// make ScrolledWindow recognize this window as scrollable
GTK_WIDGET_GET_CLASS(gwidget)->set_scroll_adjustments_signal = my_signal;
稍后,添加小部件时 ScrolledWindow 发出的信号需要由我的小部件通过信号代理方法或其他方法捕获?我不知道。
我该怎么做?
I am writing a custom widget for Gtkmm that is supposed to display a huge dataset (imagine something like a 4096x256 character datasheet).
Mostly for reasons of elegance, but also for a possible usage in a Glade/Gtk-Builder editor, I want this widget to support ScrolledWindow natively, that is, once it is set as the child of ScrolledWindow, it is recognized as a scrollable widget, allowing to set horizontal and vertical Adjustment objects on it, which it can subsequently tamper with.
It appears to do so, I need to do something like this in the constructor of my widget:
// get Gtk C type from wrapper class
GtkWidget* gwidget = this->gobj();
// imagine code here that magically creates a gobject signal,
// that we can catch in C++.
// this is actually the part which I don't know how to do.
guint my_signal = magic_way_to_create_this_signal(
&MyClass::rainbow_unicorn_signal_handler);
// make ScrolledWindow recognize this window as scrollable
GTK_WIDGET_GET_CLASS(gwidget)->set_scroll_adjustments_signal = my_signal;
Later on, the signal emitted by ScrolledWindow when the widget is added needs to be caught by my Widget through a signal proxy method or something? I have no idea.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“
magic_way_to_create_this_signal
”是g_signal_new()
。您可以在小部件的my_widget_class_init()
函数中调用它,该函数是定义类的 GObject 方式的一部分。我不太确定 Gtkmm 中的等价物是什么。另请参阅 GTK 文档中的脚注,其中这解释了为什么让一个小部件本身可滚动是如此麻烦。
您还可以将您的小部件放入
Gtk::Viewport
为其子小部件添加滚动功能。The '
magic_way_to_create_this_signal
' isg_signal_new()
. You call it in your widget'smy_widget_class_init()
function which is part of the GObject way of defining a class. I'm not exactly sure what the equivalent is in Gtkmm.See also the footnote in the GTK docs, where it is explained why making a widget natively scrollable is such a hassle.
You could also put your widget into a
Gtk::Viewport
which adds scrolling capabilities to its child widget.