我试图在Gtk :: Fixed中移动Gtk :: Button,就像在窗口管理器上的Gtk :: Window一样。按钮周围有Gtk :: GestureDrag。到目前为止,我可以移动按钮了。
如果我将“按钮”移到新位置并单击那里的按钮,则按钮将移回其原始位置。
这是缩小的代码
main.cpp
#include <gtkmm.h>
#include <iostream>
class Content: public Gtk::Fixed
{
public:
Content();
long int x, y;
protected:
Glib::RefPtr<Gtk::GestureDrag> drag;
Gtk::Button button{"Button"};
void begin_drag(double nx, double ny);
void update_drag(double nx, double ny);
void end_drag(double nx, double ny);
};
Content::Content()
{
add(button);
drag = Gtk::GestureDrag::create(button);
drag->set_button(GDK_BUTTON_PRIMARY);
drag->signal_drag_begin().connect(
sigc::mem_fun(*this, &Content::begin_drag));
drag->signal_drag_update().connect(
sigc::mem_fun(*this, &Content::update_drag));
drag->signal_drag_end().connect(
sigc::mem_fun(*this, &Content::end_drag));
}
void Content::begin_drag(double nx, double ny) {}
void Content::update_drag(double nx, double ny)
{
x = nx;
y = ny;
move(button, x, y);
}
void Content::end_drag(double nx, double ny)
{
x = nx;
y = ny;
move(button, x, y);
}
int main(int argc, char *argv[])
{
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.moving.widgets");
Gtk::Window window;
Content fixed;
window.set_default_size(480, 320);
window.add(fixed);
window.show_all_children();
app->run(window);
return 0;
}
如何在不拖动按钮的情况下使按钮保持在新位置? `