Refresh child-items in a container - GTK

冷暖自知 提交于 2019-12-25 02:37:34

问题


I am developing some kind of financial calculator in c with a graphical userinterface. I am using the gtk-lib and I have encountered one problem that right that could not be solved. Its about how to dynamically update (refresh) the window and its childs - more specifically - there is one child item in the container that is a text-label. I want this label to be changed according to what is entered in the textinputfield.

I am used to java where one easily can call a method named invalidate(). Is there a similar solution in c? Or is a solution to delete this chidlitem and and add a new one?

Thanks in advance

      #include <gtk/gtk.h>

 static GtkWidget *txt;
 static GtkWidget *window;
 static GtkWidget *vbox, *assetPrice, *strikePrice;
 static GtkWidget *asset_label;
 static GtkWidget *frame;

 static void entry_Submit(GtkWidget *widget, GtkWidget *entry)
 {
  const gchar *text = gtk_entry_get_text(GTK_ENTRY (txt));
  printf ("Result: %s\n", text);

 }

 static void CreateTextBox(GtkWidget **entry, GtkWidget *vbox)
 {
    *entry = gtk_entry_new();
    gtk_entry_set_text (GTK_ENTRY (*entry), "");
    gtk_box_pack_start (GTK_BOX (vbox), *entry, TRUE, TRUE, 0);
 }
 int main(int argc, char *argv[] )
 {
    GtkWidget *button;
    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (GTK_WIDGET (window), 300, 300);
    gtk_window_set_title (GTK_WINDOW (window), "FINANCIAL CALCULATOR");
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect_swapped (window, "delete-event", G_CALLBACK (gtk_widget_destroy), window);


    vbox = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_widget_show (vbox);

    frame = gtk_frame_new ("ASSET PRICE");
    asset_label = gtk_label_new ("stock price, stockmarketindex ...");
    gtk_container_add (GTK_CONTAINER (frame), asset_label);
    gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

    assetPrice = gtk_hbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (vbox), assetPrice);
    gtk_widget_show(assetPrice);

    frame = gtk_frame_new ("RESULT: ");
    asset_label = gtk_label_new ("... new value");
    gtk_container_add (GTK_CONTAINER (frame), asset_label);
    gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

    CreateTextBox(&txt, assetPrice);

    button = gtk_button_new_with_label("Calculate");
    g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), window);

    gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
    gtk_widget_set_can_default (button, TRUE);
    gtk_widget_grab_default (button);

     gtk_widget_show_all (window);

     gtk_main ();

 }

回答1:


The GtkEntry widget implements GtkEditable, hence it inherits its properties and signals. This in turn means you could know when its content changes by connecting a callback to the "changed" signal.




回答2:


Hook up a callbacke to the "preedit-changed" and/or "changed" and/or "activate" signal(s). Then set your label's text in the callback via gtk_label_set_text (yourlabel, gtk_entry_get_text (yourentry)); if you want to copy/paste it directly.

Note:

gtk_entry_get_text (yourentry) gives you a pointer to internal storage, so DO NOT modify it directly, use g_strdup and operate on a copy. In the above it is ok (afaik) as gtk_label_set_text will copy the text anyways. (Also don't forget to free your stuff you alocated.)

See https://developer.gnome.org/gtk3/3.4/GtkEntry.html and https://developer.gnome.org/gtk3/3.4/GtkLabel.html for details.



来源:https://stackoverflow.com/questions/20905236/refresh-child-items-in-a-container-gtk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!