如何在C中更新GTK + -3.0中的标签 [英] How do I update Labels in GTK+-3.0 in C

查看:136
本文介绍了如何在C中更新GTK + -3.0中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法使用函数

  gtk_label_set_text(); 

这是我写的:

  #include< gtk / gtk.h> 
$ b int main(int argc,char * argv [])
{
gtk_init(& argc,& argv);

GtkWidget * label;

//标签
标签= gtk_label_new(这是我的标签);

gtk_label_set_text(标签,我不能使用这个功能); (GTK_IS_LABEL(label)){
g_print(它是一个LABEL \\\
);

if
} else if(GTK_IS_WIDGET(label)){
g_print(以及至少它的一个Widget \\\
);
} else {
g_print(为什么它不是标签?!T_T \\\
);
}

gtk_main();
返回0;
}

它表示



<$从不兼容的指针类型中传递'gtk_label_set_text'的参数1
expected'struct GtkLabel *'但参数的类型为'struct GtkWidget *'
c>

当我尝试编译它时。好。我将它改为

  GtkLabel * label; 

但它不会让我使用gtk_label_new();因为编译器抱怨

 从不兼容的指针类型赋值

然后我放弃并评论// gtk_label_set_text以询问程序GTK_IS_LABEL(label);并打印出IT IS A LABEL。
我真的放弃了,现在问你,亲爱的Stack Overflow社区。

解决方案

因为它是C代码,因为 gtk_label_new 返回基类或结构:

 ╰──GtkWidget 
╰──GtkMisc
╰──GtkLabel
╰──GtkAccelLabel

演员通过一个宏完成(参见 code:

  GtkWidget * label; 

//标签
标签= gtk_label_new(这是我的标签);

gtk_label_set_text(GTK_LABEL(label),我不能使用这个func);

注意:在C ++中不需要强制转换,因为 GtkLabel 结构继承自 GtkWidget ,您可以直接将标签存储在 GtkLabel 中,但是不存在继承C。

I can't seem to be able to use the function

gtk_label_set_text();

this is what I write:

#include <gtk/gtk.h>

int main(int argc, char *argv[] )
{
  gtk_init(&argc, &argv);

  GtkWidget *label;

  //label
  label = gtk_label_new("This is my label");

  gtk_label_set_text(label, "I cannot use this func");

  if(GTK_IS_LABEL(label)){
    g_print("IT IS A LABEL\n");
  }else if (GTK_IS_WIDGET(label)){
    g_print("well at least its a Widget\n");
  }else {
    g_print("why is it not a label?! T_T\n");
  }

  gtk_main();
  return 0;
}

it says

passing argument 1 of ‘gtk_label_set_text’ from incompatible pointer type
expected ‘struct GtkLabel *’ but argument is of type ‘struct GtkWidget *’

when I try to compile it. OK. I change it to

GtkLabel *label;

but then it doesn't let me use gtk_label_new(); because the compiler complains with

assignment from incompatible pointer type

then I give up and comment //gtk_label_set_text to ask the program GTK_IS_LABEL(label); and it prints out IT IS A LABEL. I give up for real now and ask you, dear community of Stack Overflow.

As it is C code, you have apply a "cast" your pointer since gtk_label_new returns the "base class" or structure:

╰── GtkWidget
    ╰── GtkMisc
        ╰── GtkLabel
            ╰── GtkAccelLabel

The cast is done through a macro (see here) code:

GtkWidget *label;

//label
label = gtk_label_new("This is my label");

gtk_label_set_text(GTK_LABEL (label), "I cannot use this func");

Note: in C++ no need to cast since GtkLabel structure inherits from GtkWidget, you could store label in a GtkLabel directly, but there's no such thing as inheritance in C.

这篇关于如何在C中更新GTK + -3.0中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆