2

我正在尝试在 Gtk::Fixed 中移动 Gtk::Button,就像在窗口管理器上的 Gtk::Window 一样。我在按钮周围有 Gtk::GestureDrag。到目前为止,我可以移动按钮。

如果我将 Button 移动到新位置并单击那里的按钮,则该按钮将移回其原始位置。

这是一个缩小的代码

主文件

#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;
}

当我单击它而不拖动时,如何使按钮保持在新位置?`

4

0 回答 0