我收到错误,因为对于gtk_entry它需要char类型作为输入,但我想给它int类型所以首先我声明int类型然后这个int类型被声明为char类型并给予gtk_entry但我收到错误。
#include<gtk/gtk.h>
void static call(GtkWidget *widget,gpointer data)
{
g_print("\n%s\n",gtk_entry_get_text(GTK_ENTRY(data)));
gint answ=4;
const char* ans=answ; //here is the line having error
gtk_entry_set_text(GTK_ENTRY(data),ans);
gtk_editable_select_region(GTK_EDITABLE(data),0,-1);
}
int main(int agrc, char *agrv[])
{
gtk_init(&agrc,&agrv);
GtkWidget *entry,*window,*button,*hbox;
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window,"delete-event",G_CALLBACK(gtk_main_quit),NULL);
hbox=gtk_hbox_new(0,0);
gtk_container_add(GTK_CONTAINER(window),hbox);
button=gtk_button_new_with_mnemonic("write text");
entry=gtk_entry_new();
g_signal_connect(button,"clicked",G_CALLBACK(call),entry);
g_signal_connect(entry,"activate",G_CALLBACK(call),entry);
gtk_box_pack_start(GTK_BOX(hbox),button,0,0,0);
gtk_box_pack_start(GTK_BOX(hbox),entry,0,0,0);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
答案 0 :(得分:0)
如果要将整数转换为char,以便可以将此类整数作为参数传递,则执行以下操作:
#include <stdio.h>
int main(void)
{
char c[4];
int i=5;
sprintf(c, "%d", i);
printf("%s %d", c, i);
return 0;
}
使用sprintf将整数打印到char变量中,然后使用它。有一个功能itoa不是标准的,但也会帮助你。
itoa的使用方式如下:
itoa(c, i, 10);
编辑:
我认为您的代码是:
gint x=4;
char y[4];
sprintf(y, "%d", x);
gtk_entry_set_text(GTK_ENTRY(data),y);