【发布时间】:2020-10-26 10:26:39
【问题描述】:
我想在鼠标指针进入这个小部件时打开Gtk::SpinButton入口焦点,然后在指针离开时再次关闭它。
窗口仍在运行时,信号事件似乎没有响应。但是std::cout 输出是在我关闭窗口后打印出来的。
如何实现预期的小部件行为?
#include <gtkmm.h>
#include <iostream>
class SpinButtonExample : public Gtk::Window {
Gtk::Grid grid;
Gtk::Label label;
Glib::RefPtr<Gtk::Adjustment> adjustment;
Gtk::SpinButton spinbutton;
public:
SpinButtonExample();
bool on_enter_notify_event(GdkEventCrossing *);
bool on_leave_notify_event(GdkEventCrossing *);
};
SpinButtonExample::SpinButtonExample()
: label ("Hi")
, adjustment(Gtk::Adjustment::create(0, 0, 10))
, spinbutton(adjustment)
{
add_events(Gdk::ENTER_NOTIFY_MASK);
add_events(Gdk::LEAVE_NOTIFY_MASK);
spinbutton.signal_enter_notify_event().connect(
sigc::mem_fun(*this, &SpinButtonExample::on_enter_notify_event));
spinbutton.signal_leave_notify_event().connect(
sigc::mem_fun(*this, &SpinButtonExample::on_leave_notify_event));
grid.set_orientation(Gtk::ORIENTATION_HORIZONTAL);
grid.set_column_homogeneous(true);
grid.set_margin_start(10);
grid.set_margin_end(10);
grid.set_margin_top(10);
grid.set_margin_bottom(10);
grid.add(label);
grid.add(spinbutton);
add(grid);
show_all();
}
bool
SpinButtonExample::on_enter_notify_event(GdkEventCrossing *event)
{
if (event->type == GDK_SCROLL) {
std::cout << "Mouse entered\n";
spinbutton.set_can_focus();
return true;
}
return false;
}
bool
SpinButtonExample::on_leave_notify_event(GdkEventCrossing *event)
{
if (event->type == GDK_LEAVE_NOTIFY) {
std::cout << "Mouse left\n";
spinbutton.set_can_focus(false);
return true;
}
return false;
}
int
main()
{
auto application = Gtk::Application::create("test.focus.spinbutton");
SpinButtonExample test;
return application->run(test);
}
【问题讨论】:
-
如果您将
std::endl放在您的couts 中会怎样?即是否只是cout没有刷新让您认为事件被延迟到进程结束。 -
如果你想在进入时打开焦点,你为什么要检查
if (event->type == GDK_SCROLL)?显然,指针输入和滚动是不同的东西。但是您也不必费心检查,因为获取信号意味着事件具有该类型,否则为什么要为特定类型的事件命名信号...? -
感谢@underscore_d
std::endl工作。但是离开通知并没有关闭我的小部件焦点 -
什么意思?它不会关闭 focusability 吗?和/或它是否离开小部件聚焦?
-
先尝试将焦点返回到其他“默认”小部件,然后。