有时需要限制Gtkenty只能输入数字或其他特别的字符
可以对GtkEntry连接“insert-text”信号:
gulong sigid = g_signal_connect(G_OBJECT(pEntry), "insert-text", G_CALLBACK(on_entry_insert_text), NULL);
回调函数的实现:
static void on_entry_insert_text(GtkEditable *editable,
gchar *new_text,
gint new_text_length,
gint *position,
gpointer user_data)
{
const char *text = gtk_entry_get_text( GTK_ENTRY(editable) );
if( !strcmp(new_text, "0") ||
!strcmp(new_text, "1") ||
!strcmp(new_text, "2") ||
!strcmp(new_text, "3") ||
!strcmp(new_text, "4") ||
!strcmp(new_text, "5") ||
!strcmp(new_text, "6") ||
!strcmp(new_text, "7") ||
!strcmp(new_text, "8") ||
!strcmp(new_text, "9") ||
!strcmp(new_text, ".")
)
{
return;
}
else
{
gchar *result = g_utf8_strup (new_text, new_text_length);
g_signal_handlers_block_by_func (editable,
(gpointer) on_entry_insert_text, user_data);
gtk_entry_set_text (GTK_ENTRY(editable), text);
g_signal_handlers_unblock_by_func (editable,
(gpointer) on_entry_insert_text, user_data);
g_signal_stop_emission_by_name (editable, "insert_text");
g_free (result);
}
}
使用注意:
如果进行了这样的处理:在对GtkEntry使用gtk_entry_set_text()函数时需要做相应的处理,即设置前阻断信号,设置后释放;
g_signal_handler_block(pEntry, sigid);
gtk_entry_set_text(GTK_ENTRY(pEntry), “text”);
g_signal_handler_unblock(pEntry, sigid);
阅读(2455) | 评论(0) | 转发(0) |