有人能帮帮我吗?我有C编程技能,我正在尝试使用gtk编程gui。我希望,通过从小部件(例如按钮)触发动作(回调函数),我可以更改属于任何其他小部件的任何条件或特征。就像在回调函数中允许完全可见性一样,好像所有gui小部件中的所有变量(比如单个主应用程序窗口)都是全局的。我怎样才能找到最接近完成此任务的方法?到目前为止,我尝试使用gtk编程时发现基本概念的步骤令我感到沮丧,当我尝试制作比简单的Hello World教程更复杂的东西。我也尝试使用:
/* out of main */
struct data_widgets
{
struct data_widgets * myp;
gchar **loc ; // LOCAL name
gchar **rem ; // REMOTE name
gchar **pmte ; // Plain message to encrypt filename
gchar **lem ; // Local encrypted message filename
gchar **emr ; // Encrypted message received filename
gchar **pmr ; // Plain message received filename
gchar **lopk ; // Local owner public key filename
gchar **crpk ; // Current remote public key filename
};
int main(int argc, char *argv[])
{
gchar loc_str[100] = "*"; /* LOCAL name for filechoose filter */
gchar rem_str[100] = "*"; /* REMOTE name idem */
gchar pmte_str[100]= "plainmsgtoencrypt"; /* Plain message to encrypt filename */
gchar lem_str[100] = "localencmsg"; /* Local encrypted message filename */
gchar emr_str[100] = "encmsgreceiv"; /* Encrypted message received filename */
gchar pmr_str[100] = "plainreceiv"; /* Plain message received filename */
gchar lopk_str[100]= "locownpubkey"; /* Local owner public key filename */
gchar crpk_str[100]= "remotpubkey"; /* Current remote public key filename */
struct data_widgets mydata;
mydata.loc = &loc_str;
mydata.rem = &rem_str;
mydata.pmte = &pmte_str;
mydata.lem = &lem_str;
mydata.emr = &emr_str;
mydata.pmr = &pmr_str;
mydata.lopk = &lopk_str;
mydata.crpk = &crpk_str;
mydata.myp = &mydata;
/* in main */
....
/* in my callback */
struct data_widgets *pp = (struct data_widgets *) data;
/*passing gpointer data as &mydata.myp, and doing (*pp)->(any pointer) this try fail*/
答案 0 :(得分:0)
根据我的理解,你想从回调中访问小部件,而不必随身携带一堆全局变量。你有一些选择。
您可以在单个结构中打包数据和小部件,并将其作为user_data
传递给回调。
struct snapshot {
struct data_widgets data;
GtkWidget *entry;
GtkWidget *box;
GtkWidget *label;
GtkWidget *whatever;
};
/* Now fill and use the above struct as user_data */
应在用于创建UI内容的代码中初始化GtkWidget
字段。
您还可以使用gtk_widget_set_name()为每个相关窗口小部件设置名称(Glade会自动为每个元素设置名称)。一旦您需要一个小部件,只需使用gtk_widget_get_name()。
static void a_callback(GtkWidget *widget, gpointer user_data)
GtkWidget *an_entry = NULL;
GtkWidget *toplevel = gtk_widget_get_toplevel(widget);
if (gtk_widget_is_toplevel(toplevel))
an_entry = gtk_widget_get_name(toplevel, "name of the entry");
g_return_if_fail(an_entry != NULL);
/* ... do something with an_entry ... */
}
您还可以遍历窗口小部件层次结构,就像它是DOM一样,或者使用g_object_set_data()或上述任何有效混合将数据直接绑定到窗口小部件。
使用g_object_set_data()
,您可以将任意数据附加到任何GObject
个实例,例如:
gchar *text;
/* Bind some data to widget */
g_object_set_data(G_OBJECT(widget), "pmte", "plainmsgtoencrypt");
g_object_set_data(G_OBJECT(widget), "lem", "localencmsg");
text = g_object_get_data(G_OBJECT(widget), "pmte");
g_print("%s\n", text); /* Print "plainmsgtoencrypt" */
g_free(text);
text = g_object_get_data(G_OBJECT(widget), "lme");
g_print("%s\n", text); /* Print "localencmsg" */
g_free(text);
你的代码在很多方面都很混乱:我建议你忘记你从C学到的东西并从零开始重启。您的数据的间接级别高于所需的级别,myp
是伪造的,您指定数组的大小并初始化它。
回答您的评论:以下代码是自包含的,不会引发您看到的错误。
#include <gtk/gtk.h>
struct data_widgets {
gchar **crpk;
};
void callback(GtkWidget *widget, gpointer user_data) {
struct data_widgets *data = user_data;
g_print("%s\n", data->crpk); /* Prints remotpubkey */
}
int main(int argc, char *argv[]) {
gchar crpk_str[100] = "remotpubkey";
struct data_widgets mydata;
mydata.crpk = &crpk_str;
callback(NULL, &mydata);
return 0;
}
相同的代码,以清醒的方式清理:
#include <gtk/gtk.h>
typedef struct {
gchar *crpk;
} data_t;
void callback(GtkWidget *widget, gpointer user_data) {
data_t *data = user_data;
g_print("%s\n", data->crpk); /* Prints remotpubkey */
}
int main() {
data_t mydata;
mydata.crpk = "remotpubkey";
callback(NULL, &mydata);
return 0;
}