当前位置: 技术问答>linux和unix
高分求助gtk_text_buffer_set_text参数中字符编码问题
来源: 互联网 发布时间:2016-11-09
本文导语: void recv_func(gpointer user_data) { …… char recv_text[MAXSIZE]; gchar *text; text=(gchar *)malloc(MAXSIZE); …… text_buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(user_data));/*user_data为textview2*/ text = g_convert(recv_text,sizeof(recv_te...
void recv_func(gpointer user_data)
{
……
char recv_text[MAXSIZE];
gchar *text;
text=(gchar *)malloc(MAXSIZE);
……
text_buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(user_data));/*user_data为textview2*/
text = g_convert(recv_text,sizeof(recv_text),"utf-8","CP850",NULL,NULL,NULL);
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(text_buffer),text,MAXSIZE);
g_print("%s,%s",recv_text,text);/*显示均正常*/
gtk_text_view_set_buffer(GTK_TEXT_VIEW(textview1),buffer); /*无显示*/
}
以上代码在Vista上运行调试时,提示验证gtk_text_buffer_set_text中的参数验证为utf-8时失败(Gtk-CRITICAL **: gtk_text_buffer_emit_insert: assertion 'g_utf8_validate (text,len,NULL)' failed),如何解决呢?
|
gchar *text;
text = g_strdup_printf("%s",recv_text);
textview_set_text(textview,text,-1);
试试看
text = g_strdup_printf("%s",recv_text);
textview_set_text(textview,text,-1);
试试看
|
这个问题一般是因为text为空,或者没有结尾之类的,要用""而不是'',在linux下我是这么用的
void textview_set_text(GtkTextView *textview,gchar *text,unsigned long size)
{
GtkTextBuffer *buffer;
if(!text)
return;
buffer = gtk_text_view_get_buffer(textview);
gtk_text_buffer_set_text(buffer,text,size);
}
void textview_set_text(GtkTextView *textview,gchar *text,unsigned long size)
{
GtkTextBuffer *buffer;
if(!text)
return;
buffer = gtk_text_view_get_buffer(textview);
gtk_text_buffer_set_text(buffer,text,size);
}
|
up
|
gchar* textview_get_text(GtkTextView *textview)
{
GtkTextBuffer *buffer;
GtkTextIter start,end;
/*get the buffer's text and save as a file*/
buffer = gtk_text_view_get_buffer(textview);
gtk_text_buffer_get_start_iter(buffer,&start);
gtk_text_buffer_get_end_iter(buffer,&end);
return gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
}
{
GtkTextBuffer *buffer;
GtkTextIter start,end;
/*get the buffer's text and save as a file*/
buffer = gtk_text_view_get_buffer(textview);
gtk_text_buffer_get_start_iter(buffer,&start);
gtk_text_buffer_get_end_iter(buffer,&end);
return gtk_text_buffer_get_text(buffer,&start,&end,FALSE);
}
|
帮顶吧。