当我在 Glade 中指定函数名称时,是否必须手动附加 gtk 信号处理程序?

发布于 2024-09-30 07:54:24 字数 421 浏览 1 评论 0原文

我正在编写我的第一个 gtk 程序,使用 gtkmm 和glade。 我制作了一个文件选择器按钮,它有一个名为文件集的信号 所以我将其设置为我假设的函数名称,我希望它在选择文件时调用。 但后来我看到这里: http://library.gnome.org /devel/gtkmm-tutorial/unstable/sec-builder-accessing-widgets.html.en

他们手动获取对话框小部件并在代码中设置按钮信号处理程序。 哪种方法是正确的?

虽然我在这里提供任何优秀示例的链接都会很方便,但它们似乎很少而且相距甚远。谢谢。

I'm writing my first gtk program, using gtkmm, and glade.
I made a filechooserbutton and it has a signal called file-set
So I set that to what I assume is the function name I want it to call when the file is chosen.
But then I see here:
http://library.gnome.org/devel/gtkmm-tutorial/unstable/sec-builder-accessing-widgets.html.en

That they're manually getting the dialog widget and setting a button signal handler in the code.
Which is the right way to do it?

And while I'm here any links to good examples would be handy, they seem to be few and far between. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2024-10-07 07:54:24

我就是这样做的:

// create the UI
refUI = Gtk::Builder::create();
refUI->add_from_file(grq::GLADE_FILE);

// grab your widget
refUI->get_widget("but_new", but_new); // Gtk::ToolButton *but_new;
but_new->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_new_game));

// your signal handler looks something like this :)
void MainWindow::on_new_game() {}

编辑:

基本上,*this 是您将在其上调用信号处理程序函数的对象。

这就是我的 main 的样子:

int main(int argc, char **argv) {

    Gtk::Main       kit(argc, argv);
    MainWindow      main_window;

    kit.run(*main_window.window);

    return 0;
}

MainWindow 基本上是一个包装 GtkWindow 并定义小部件的类,a.洛杉矶:

class MainWindow
{

private:
Glib::RefPtr<Gtk::Builder> refUI;

//
// Widgets
//

Gtk::ToolButton *but_about;

public:

// The window. This is public so we can hook into events and
// call kit.run(window) against it, if needed.
Gtk::Window *window;


MainWindow()
{
    // Load the data for this window and it's widgets.
    refUI = Gtk::Builder::create();
    refUI->add_from_file(grq::GLADE_FILE);


    // The window
    refUI->get_widget("main_window", window);


    // Widgets              
    refUI->get_widget("but_about", but_about);
    but_about->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_about));

            ...

}


virtual ~MainWindow()
{
    if (window != NULL)
    {
        delete window; // Frees all the children for the window, too.
    }
}

    virtual void on_about()
    {
            // stuff
    }

};

希望这有帮助!

This is how I did it:

// create the UI
refUI = Gtk::Builder::create();
refUI->add_from_file(grq::GLADE_FILE);

// grab your widget
refUI->get_widget("but_new", but_new); // Gtk::ToolButton *but_new;
but_new->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_new_game));

// your signal handler looks something like this :)
void MainWindow::on_new_game() {}

edit:

Basically the *this is the object on which you will be calling the function your signal handler.

This is what my main looks like:

int main(int argc, char **argv) {

    Gtk::Main       kit(argc, argv);
    MainWindow      main_window;

    kit.run(*main_window.window);

    return 0;
}

MainWindow is basically a class that wraps GtkWindow and defines the widgets, a. la.:

class MainWindow
{

private:
Glib::RefPtr<Gtk::Builder> refUI;

//
// Widgets
//

Gtk::ToolButton *but_about;

public:

// The window. This is public so we can hook into events and
// call kit.run(window) against it, if needed.
Gtk::Window *window;


MainWindow()
{
    // Load the data for this window and it's widgets.
    refUI = Gtk::Builder::create();
    refUI->add_from_file(grq::GLADE_FILE);


    // The window
    refUI->get_widget("main_window", window);


    // Widgets              
    refUI->get_widget("but_about", but_about);
    but_about->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::on_about));

            ...

}


virtual ~MainWindow()
{
    if (window != NULL)
    {
        delete window; // Frees all the children for the window, too.
    }
}

    virtual void on_about()
    {
            // stuff
    }

};

Hope this helps!

你怎么敢 2024-10-07 07:54:24

我在另一个 stackoverflow 问题中事后找到了我的问题的答案。

但我不记得是哪一个了。

答案似乎是您必须以编程方式将信号处理程序添加到代码中的小部件中,gtkbuilder 不会为您做这件事。

I found the answer to my question as an afterthought in another stackoverflow question.

But I don't remember which one it was.

The answer seems to be that you have to programmatically add the signal handler to the widget in your code, the gtkbuilder won't do it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文