如何将 Gtkmm 信号连接到另一个 class 中的函数?
How can I connect a Gtkmm signal to a fuction in another class?
我是 c++ 和 Gtkmm3 的新手。我需要在另一个 class 中设置信号,如果不使用小部件 class 中的函数加载 class,我将如何做到这一点。
我不想在 ButtonBox 中重新加载整个 FixedLayout
在classButtonBox我不想要
FixedLayout instance;
instance.move_widget();
下面是我的代码
layout.cpp
#include "layout.h"
#include <iostream>
FixedLayout::FixedLayout()
{
fixed.put(box, 0, 0);
add(fixed);
}
void FixedLayout::move_widget(int x, int y)
{
fixed.move(*fixed.get_focus_child(), x, y);
}
ButtonBox::ButtonBox()
{
set_size_request(320, 480);
header.set_size_request(-1, 24);
body.set_size_request(-1, 456);
pack_start(header);
dragger = Gtk::GestureDrag::create(header);
dragger->set_button(GDK_BUTTON_PRIMARY);
dragger->set_propagation_phase(Gtk::PHASE_BUBBLE);
dragger->signal_drag_update().connect(sigc::mem_fun(*this, &ButtonBox::update_drag));
pack_end(body);
}
void ButtonBox::update_drag(bool x, bool y)
{
FixedLayout::move_widget(x, y);
}
布局.h
#ifndef FIXEDLAYOUT_H
#define FIXEDLAYOUT_H
#include <gtkmm.h>
class ButtonBox: public Gtk::VBox
{
public:
ButtonBox();
protected:
Gtk::Button header{"Header"}, body{"Body"};
Glib::RefPtr<Gtk::GestureDrag> dragger;
void update_drag(bool x, bool y);
private:
};
class FixedLayout: public Gtk::ScrolledWindow
{
public:
FixedLayout();
//virtual ~bar();
protected:
void move_widget(int x, int y);
Gtk::Fixed fixed;
ButtonBox box;
private:
};
#endif // FIXEDLAYOUT_H
dragger->signal_drag_update().connect(sigc::mem_fun(*this, &ButtonBox::update_drag));
以下是将信号连接到回调方法的方法。 this
是指向 class 的实际实例的指针。那么如何连接到另一个实例 class?
使用指针
如果需要将信号连接到另一个 class 实例的方法,则需要提供指向该实例的指针而不是 this
。在线内容:
signal().connect(sigc::mem_fun(*p, &::callback_name));
这里的p
是一个通用指针,让它成为你的class的一个属性。请注意,这会使两个 class 相互依赖,因此如果您想让代码可重用,这通常不是一个好主意。
在你的情况下,你需要在 ButtonBox
.
中放置一个指向 FixedLayout
实例的指针
class ButtonBox: public Gtk::VBox
{
private: #could be whatever
FixedLayout *ptofl;
};
在你的构造函数中:
whatever_signal().connect(sigc::mem_fun(*ptofl, &FixedLayout::name_of_the_method));
继承
另一种方法是使 ButtonBox
child of FixedLayout
:
class ButtonBox: public Gtk::VBox, public FixedLayout
您需要颠倒两个 class 的顺序定义。然后从 ButtonBox
您可以访问 FixedLayout
的受保护和 public 方法,并且在您的代码中您只能使用 ButtonBox
实例。将信号连接到回调时,您可以照常使用 this
指针。
我是 c++ 和 Gtkmm3 的新手。我需要在另一个 class 中设置信号,如果不使用小部件 class 中的函数加载 class,我将如何做到这一点。
我不想在 ButtonBox 中重新加载整个 FixedLayout
在classButtonBox我不想要
FixedLayout instance;
instance.move_widget();
下面是我的代码
layout.cpp
#include "layout.h"
#include <iostream>
FixedLayout::FixedLayout()
{
fixed.put(box, 0, 0);
add(fixed);
}
void FixedLayout::move_widget(int x, int y)
{
fixed.move(*fixed.get_focus_child(), x, y);
}
ButtonBox::ButtonBox()
{
set_size_request(320, 480);
header.set_size_request(-1, 24);
body.set_size_request(-1, 456);
pack_start(header);
dragger = Gtk::GestureDrag::create(header);
dragger->set_button(GDK_BUTTON_PRIMARY);
dragger->set_propagation_phase(Gtk::PHASE_BUBBLE);
dragger->signal_drag_update().connect(sigc::mem_fun(*this, &ButtonBox::update_drag));
pack_end(body);
}
void ButtonBox::update_drag(bool x, bool y)
{
FixedLayout::move_widget(x, y);
}
布局.h
#ifndef FIXEDLAYOUT_H
#define FIXEDLAYOUT_H
#include <gtkmm.h>
class ButtonBox: public Gtk::VBox
{
public:
ButtonBox();
protected:
Gtk::Button header{"Header"}, body{"Body"};
Glib::RefPtr<Gtk::GestureDrag> dragger;
void update_drag(bool x, bool y);
private:
};
class FixedLayout: public Gtk::ScrolledWindow
{
public:
FixedLayout();
//virtual ~bar();
protected:
void move_widget(int x, int y);
Gtk::Fixed fixed;
ButtonBox box;
private:
};
#endif // FIXEDLAYOUT_H
dragger->signal_drag_update().connect(sigc::mem_fun(*this, &ButtonBox::update_drag));
以下是将信号连接到回调方法的方法。 this
是指向 class 的实际实例的指针。那么如何连接到另一个实例 class?
使用指针
如果需要将信号连接到另一个 class 实例的方法,则需要提供指向该实例的指针而不是 this
。在线内容:
signal().connect(sigc::mem_fun(*p, &::callback_name));
这里的p
是一个通用指针,让它成为你的class的一个属性。请注意,这会使两个 class 相互依赖,因此如果您想让代码可重用,这通常不是一个好主意。
在你的情况下,你需要在 ButtonBox
.
FixedLayout
实例的指针
class ButtonBox: public Gtk::VBox
{
private: #could be whatever
FixedLayout *ptofl;
};
在你的构造函数中:
whatever_signal().connect(sigc::mem_fun(*ptofl, &FixedLayout::name_of_the_method));
继承
另一种方法是使 ButtonBox
child of FixedLayout
:
class ButtonBox: public Gtk::VBox, public FixedLayout
您需要颠倒两个 class 的顺序定义。然后从 ButtonBox
您可以访问 FixedLayout
的受保护和 public 方法,并且在您的代码中您只能使用 ButtonBox
实例。将信号连接到回调时,您可以照常使用 this
指针。