【问题标题】:How to insert UTF8 literal in GtkTextView?如何在 GtkTextView 中插入 UTF8 文字?
【发布时间】:2012-04-28 15:35:24
【问题描述】:

我想将 插入到 GtkTextView 小部件中。在 Python + pyGTK 中,这一行就足够了:

self.__textBuffer.insert_at_cursor(u'\u2022')

因为我正在将我的应用程序重写为 C,所以我需要翻译这行代码。

我想这样做:

gtk_text_buffer_insert_at_cursor(textBuffer, "\x20\x22", 2);

但它不起作用,只插入了2个字符"*

如何将 u'\u2022' 从 Python 翻译成 C?

【问题讨论】:

  • 为什么不直接做 gtk_text_buffer_insert_at_cursor(textBuffer, "•", strlen("•"))?

标签: python c utf-8 gtk


【解决方案1】:

您需要对其进行 UTF-8 编码,因为 GTK+ 的编码是 UTF-8。

因此:

const char bullet_utf8[] = "\xe2\x80\xa2";
gtk_text_buffer_insert_at_cursor(textBuffer, bullet_utf8, strlen(bullet_utf8));

你可以看到这个字符here, for instance的UTF-8编码。

【讨论】:

    【解决方案2】:

    如何将 u'\u2022' 从 Python 翻译成 C?

    如果你用c++编译器编译,编译器有c++0x或者c++11规范(可以用g++ -std=c++0x指定),可以编写如下代码。

    gtk_text_buffer_insert_at_cursor(textbuffer, u8"\u2022", -1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多