在Gtk中进行基本聊天

时间:2013-02-21 22:30:20

标签: c gtk

所以,我有三个标签和一个输入框。我的目标是每次输入输入框并按Enter键,文本将出现在第三个标签中。如果再次执行此操作,文本将显示在第三个标签中,旧文本将移动到第二个标签中,依此类推(因此标签会显示您键入的最后三个消息。)

Here is the ChatInfo Struct:

typedef struct chat_info ChatInfo;
struct chat_info {
GtkWidget *text_line1;
GtkWidget *text_line2;
GtkWidget *text_line3;
GtkWidget *entry;

};

这是初始化聊天框的功能:

static void get_chatbox(GtkWidget ** vbox, gpointer data) {

GuiInfo *g = (GuiInfo *) data;

ChatInfo *c;
c = (ChatInfo *) malloc(sizeof(ChatInfo));

g->chat = c;

GtkWidget *text_line1;
GtkWidget *text_line2;
GtkWidget *text_line3;

GtkWidget *entry;
GtkEntryBuffer *buffer;

text_line1 = gtk_label_new("");
text_line2 = gtk_label_new("");
text_line3 = gtk_label_new("");

buffer = gtk_entry_buffer_new(NULL, -1);
entry = gtk_entry_new_with_buffer(buffer);


gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);


gtk_box_pack_end(GTK_BOX(*vbox), entry, FALSE, FALSE, 1);
gtk_box_pack_end(GTK_BOX(*vbox), text_line3, FALSE, FALSE, 1);
gtk_box_pack_end(GTK_BOX(*vbox), text_line2, FALSE, FALSE, 1);
gtk_box_pack_end(GTK_BOX(*vbox), text_line1, FALSE, FALSE, 1);


gtk_misc_set_alignment((GtkMisc *) text_line1, .01, .5);
gtk_misc_set_alignment((GtkMisc *) text_line2, .01, .5);
gtk_misc_set_alignment((GtkMisc *) text_line3, .01, .5);
gtk_misc_set_alignment((GtkMisc *) entry, .01, .5);

g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(send_message), g);
}

这是回调:

static void send_message(GtkWidget *window, gpointer data){
GuiInfo *g = (GuiInfo *) data;


ChatInfo *c = g->chat;

const gchar *message = gtk_entry_get_text(GTK_ENTRY(c->entry));

const gchar *oldtext3 = gtk_label_get_text(c->text_line3);
const gchar *oldtext2 = gtk_label_get_text(c->text_line2);



gtk_label_set_text(c->text_line3, message);
gtk_label_set_text(c->text_line2, oldtext3);
gtk_label_set_text(c->text_line1, oldtext2);


}

我在这条线上遇到了一个段错误,并且不确定原因:

const gchar * message = gtk_entry_get_text(GTK_ENTRY(c-> entry));

2 个答案:

答案 0 :(得分:3)

你malloc c和你gtk_label_new你的text_line1等... 但你永远不会做

c->text_line1 = text_line1;
get_chatbox()中的

我认为这与入境相同:)

因此,当您使用c->条目

时,请使用单位化值

所以结果是

static void get_chatbox(GtkWidget ** vbox, gpointer data)
{
     GuiInfo*        g = (GuiInfo*)data;
     ChatInfo*       c;
     GtkWidget*      text_line1;
     GtkWidget*      text_line2;
     GtkWidget*      text_line3;
     GtkWidget*      entry;
     GtkEntryBuffer* buffer;

     c = malloc(sizeof(*ChatInfo));
     g->chat = c;
     text_line1 = gtk_label_new("");
     text_line2 = gtk_label_new("");
     text_line3 = gtk_label_new("");
     buffer = gtk_entry_buffer_new(NULL, -1);
     entry = gtk_entry_new_with_buffer(buffer);
     gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
     //------MY ADD-------
     c->text_line1 = text_line1;
     c->text_line2 = text_line2;
     c->text_line3 = text_line3;
     c->entry = entry;
     //-------------------

     gtk_box_pack_end(GTK_BOX(*vbox), entry, FALSE, FALSE, 1);
     gtk_box_pack_end(GTK_BOX(*vbox), text_line3, FALSE, FALSE, 1);
     gtk_box_pack_end(GTK_BOX(*vbox), text_line2, FALSE, FALSE, 1);
     gtk_box_pack_end(GTK_BOX(*vbox), text_line1, FALSE, FALSE, 1);


     gtk_misc_set_alignment((GtkMisc *) text_line1, .01, .5);
     gtk_misc_set_alignment((GtkMisc *) text_line2, .01, .5);
     gtk_misc_set_alignment((GtkMisc *) text_line3, .01, .5);
     gtk_misc_set_alignment((GtkMisc *) entry, .01, .5);
     g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(send_message), g);
}

答案 1 :(得分:1)

在get_chatbox中,您在本地声明:

GtkWidget *text_line1;
GtkWidget *text_line2;
GtkWidget *text_line3;

GtkWidget *entry;

我认为你的意思是引用c的成员,所以你不需要声明这些局部变量,而是使用:

c->text_line1 = gtk_label_new("");
c->text_line2 = gtk_label_new("");
c->text_line3 = gtk_label_new("");

c->entry = gtk_entry_new_with_buffer(buffer);