C 如何在gtk上添加定时器按钮信号?
所以我在用glade设计一个虚拟numpad。它现在可以工作了,但我想让按钮以t9的方式打印字母,但我不知道如何计时。。。请帮忙。到目前为止,我的代码是:C 如何在gtk上添加定时器按钮信号?,c,ubuntu,gtk,glade,C,Ubuntu,Gtk,Glade,所以我在用glade设计一个虚拟numpad。它现在可以工作了,但我想让按钮以t9的方式打印字母,但我不知道如何计时。。。请帮忙。到目前为止,我的代码是: #include <gtk/gtk.h> #include <string.h> GtkWidget *g_entry1; int main(int argc, char *argv[]) { GtkBuilder *build
#include <gtk/gtk.h>
#include <string.h>
GtkWidget *g_entry1;
int main(int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "labtest.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
gtk_builder_connect_signals(builder, NULL);
g_entry1 = GTK_WIDGET(gtk_builder_get_object(builder,"entry1"));
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
return 0;
}
void on_btn1_clicked()
{
gchar *num;
char num_arr[20];
num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,"1");
gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}
void on_btn2_clicked()
{
gchar *num;
char num_arr[20];
num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,"2");
gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}
void on_btn3_clicked()
{
gchar *num;
char num_arr[20];
num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,"3");
gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}
void on_btn4_clicked()
{
gchar *num;
char num_arr[20];
num = gtk_entry_get_text(GTK_ENTRY(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,"4");
gtk_entry_set_text(GTK_ENTRY(g_entry1), num_arr);
}
#包括
#包括
GtkWidget*g_entry1;
int main(int argc,char*argv[])
{
GtkBuilder*builder;
GtkWidget*窗口;
gtk_init(&argc,&argv);
builder=gtk_builder_new();
gtk_builder_从_文件添加_(builder,“labtest.glade”,NULL);
窗口=GTK_小部件(GTK_生成器_获取对象(生成器,“窗口1”);
gtk_builder_connect_信号(builder,NULL);
g_entry1=GTK_小部件(GTK_builder_get_对象(builder,entry1));
g_object_unref(建筑商);
gtk_widget_show(窗口);
gtk_main();
返回0;
}
单击时无效()
{
gchar*num;
charnum_arr[20];
num=gtk_entry_get_text(gtk_entry(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,“1”);
gtk_条目设置文本(gtk_条目(g_条目1),num_arr);
}
单击时无效()
{
gchar*num;
charnum_arr[20];
num=gtk_entry_get_text(gtk_entry(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,“2”);
gtk_条目设置文本(gtk_条目(g_条目1),num_arr);
}
单击时无效()
{
gchar*num;
charnum_arr[20];
num=gtk_entry_get_text(gtk_entry(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,“3”);
gtk_条目设置文本(gtk_条目(g_条目1),num_arr);
}
单击时无效()
{
gchar*num;
charnum_arr[20];
num=gtk_entry_get_text(gtk_entry(g_entry1));
strcpy(num_arr,num);
strcat(num_arr,“4”);
gtk_条目设置文本(gtk_条目(g_条目1),num_arr);
}
其他按钮也是如此(我想把它作为评论,但遗憾的是我没有50%的声誉)
请你澄清一下你的问题
我想让按钮以t9的方式打印字母,但我不知道如何计时
这到底是什么意思?此外,您还可以提供指向labtest.glade文件的链接
编辑: 好的,您可以使用
struct timespec
,您的按钮2可能有类似的功能:
您可能想更改最大延迟,并且有timespec\u减法
和timespec\u到浮动的代码
:
void timespec_normalize(struct timespec *t) {
int n;
const int sn = 1000000000;
n = t->tv_nsec / sn;
t->tv_sec += n;
t->tv_nsec -= n * sn;
return;
}
int timespec_subtract(struct timespec *r, struct timespec *x,
struct timespec *y) {
timespec_normalize(x);
timespec_normalize(y);
if(x->tv_nsec < y->tv_nsec) {
x->tv_sec--;
x->tv_nsec += 1000000000;
}
r->tv_sec = x->tv_sec - y->tv_sec;
r->tv_nsec = x->tv_nsec - y->tv_nsec;
return 0;
}
float timespec_to_float(struct timespec *t) {
return (float)(t->tv_sec) + (float)(t->tv_nsec) / 1000000000.0f;
}
void timespec\u规范化(struct timespec*t){
int n;
常数int sn=100000000;
n=t->tv\u nsec/sn;
t->tv_sec+=n;
t->tv\u nsec-=n*sn;
返回;
}
int timespec_减法(struct timespec*r,struct timespec*x,
结构时间规格*y){
timespec_归一化(x);
timespec_归一化(y);
如果(x->tv\u nsectv\u nsec){
x->tv_秒--;
x->tv\u nsec+=100000000;
}
r->tv_sec=x->tv_sec-y->tv_sec;
r->tv\u nsec=x->tv\u nsec-y->tv\u nsec;
返回0;
}
浮动时间段到浮动时间段(结构时间段*t){
返回(浮动)(t->tv_sec)+(浮动)(t->tv_nsec)/100000000.0f;
}
因此,您只需检查实际的延迟是否大于数字上的最大延迟文本,即如果我按两次按钮,我应该得到A,如果我按三次,我应该得到3,依此类推
void timespec_normalize(struct timespec *t) {
int n;
const int sn = 1000000000;
n = t->tv_nsec / sn;
t->tv_sec += n;
t->tv_nsec -= n * sn;
return;
}
int timespec_subtract(struct timespec *r, struct timespec *x,
struct timespec *y) {
timespec_normalize(x);
timespec_normalize(y);
if(x->tv_nsec < y->tv_nsec) {
x->tv_sec--;
x->tv_nsec += 1000000000;
}
r->tv_sec = x->tv_sec - y->tv_sec;
r->tv_nsec = x->tv_nsec - y->tv_nsec;
return 0;
}
float timespec_to_float(struct timespec *t) {
return (float)(t->tv_sec) + (float)(t->tv_nsec) / 1000000000.0f;
}