将附加参数传递给 gtk 函数 [英] Passing additional arguments to gtk function

查看:22
本文介绍了将附加参数传递给 gtk 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用 gtk+ 3.0 制作 GUI.我想将一个简单的参数(一个整数)传递给回调函数,这样当我按下按钮时,参数的值就会发生变化.这是我的代码:

I'm trying to learn how to make GUIs using gtk+ 3.0. I want to pass a simple argument, an integer, to a callback function, so that when I press the button the value of the argument changes. Here's my code:

#include <gtk/gtk.h>

void buttonFunction(GtkWidget * widget, gpointer data, int & n){
    n = 1;
}

int main(int argc, char ** argv){
    int n = 0;
    GtkWidget * window;
    GtkWidget * button; 

    gtk_init(&argc,&argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label("Osss");

    gtk_container_add(GTK_CONTAINER(window),button);
    gtk_widget_show_all(window);

    g_signal_connect(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttonFunction), n);

    gtk_main();

    return 0;
}

我发现传递参数的唯一方法是作为指针:

The only way I found to pass the argument was as a pointer:

void buttonFunction(GtkWidget * widget, gpointer * data);
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttonFunction), &n);

如何以这种方式传递多个参数?

How do I pass multiple arguments this way tho?

推荐答案

要传递多个参数,你定义一个结构,填充它,并传递一个指向该结构的指针作为 gpointer user_data 参数g_signal_connect,这是最后一个参数.然后在您的回调中,您只需将 user_data 参数转换为指向您的结构的指针.

To pass multiple arguments, you define a structure, fill it, and pass a pointer to the structure as the gpointer user_data parameter of the g_signal_connect, which is the last parameter. Then in your callback, you just cast the user_data parameter to a pointer to your structure.

int main (int argc, char **argv)
{
    int n = 0;
    GtkWidget *window;
    GtkWidget *button; 

    gtk_init (&argc,&argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    button = gtk_button_new_with_label ("Osss");

    gtk_container_add (GTK_CONTAINER(window), button);
    gtk_widget_show_all (window);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

    /* Here's the magic: you pass a pointer to the variable you'd like to modify
     * in the callback, be it a simple variable or a struct */
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(buttonFunction), &n);

    gtk_main();

    return 0;
}

void on_button_clicked (GtkButton *button, gpointer user_data) /* No extra parameter here ! */
{
    /* you cast to the type of what you passed as last argument of g_signal_connect */ 
    int *pn = user_data; 
    *pn = 1;
}

您必须使用文档中定义的回调的签名(查看 GtkButton 文档的信号"部分),您无法编造.顺便说一句,您不能将 n 作为引用而不是指针传递.如果您想在 C++ 中使用 GTK,请查看 GTKmm.

You MUST use the signature of the callback defined in the documentation (look at the "signals" section of the documentation for GtkButton), you can't make things up. BTW, you can't pass n as a reference instead of a pointer. If you want to use GTK in C++, give a look at GTKmm.

这篇关于将附加参数传递给 gtk 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆