How to manipulate Widgets from the main window through a signal function ( C and GTK+)?

Asked

Viewed 216 times

4

I have very little experience and am coding a program for a college job.

I am using the GTK+ 2.0 library. There are several widgets in the main window, among them a button that I connected to a function.

The problem is that I would like this function (connected to the button) to change the widgets in the main window.

For example: When I click the button, it calls its function, and it writes a string obtained from an entry (text box in the main window) into a text widget (text display screen, also in the main window).

But in gtk, we don’t call the signal function directly (it’s yes through g_signal_connect(...)), so I don’t know how to pass vestments or receive returns...

How can I get around that ?

  • 1

    Hi Lucas, could you prepare a code example demonstrating your difficulty? You can write a function of callback that gets the text of entry using gtk_entry_get_text() and set the text in your other widget with its respective September.

  • 1

    To make it clear, you don’t need inputs and returns, just have widgets accessible in callback call scope.

1 answer

1

You can define your widgets in a structure and call them from within the function you created -- including organizing your application --, but calling by g_signal_connect(), and then connect a widget to another. An example of code in which the text of a entry is put on a label after a button be clicked:

#include <gtk/gtk.h>

typedef struct MinhaJanela {

    GtkWidget *window;
    GtkWidget *botao;
    GtkWidget *texto;
    GtkWidget *label;
    GtkWidget *fixed;

} JanelaPrincipal;

static void botao_clicado(GtkWidget *, gpointer);

int main(int argc, char *argv[])
{
    JanelaPrincipal janela;

    gtk_init(&argc, &argv);

    janela.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title(GTK_WINDOW(janela.window), "Minha Janela");
    gtk_window_set_default_size(GTK_WINDOW(janela.window), 300, 80);
    gtk_window_set_position(GTK_WINDOW(janela.window), GTK_WIN_POS_CENTER);

    janela.fixed = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(janela.window), janela.fixed);

    janela.texto = gtk_entry_new();
    gtk_fixed_put(GTK_FIXED(janela.fixed), janela.texto, 15, 15);
    gtk_widget_set_size_request(janela.texto, 90, 30);

    janela.botao = gtk_button_new_with_label("Botão");
    gtk_fixed_put(GTK_FIXED(janela.fixed), janela.botao, 115, 15);
    gtk_widget_set_size_request(janela.botao, 80, 30);

    janela.label = gtk_label_new("Label");
    gtk_fixed_put(GTK_FIXED(janela.fixed), janela.label, 190, 15);
    gtk_widget_set_size_request(janela.label, 100, 30);

    /* Sinais */
    g_signal_connect(GTK_OBJECT(janela.botao), (gpointer) "clicked", 
        G_CALLBACK(botao_clicado), &janela);

    g_signal_connect_swapped(G_OBJECT(janela.window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all(janela.window);
    gtk_main();

    return 0;
}

/* Função chamada */
static void botao_clicado(GtkWidget *widget, gpointer data)
{
    gtk_label_set_text( GTK_LABEL( ((JanelaPrincipal *)data) -> label ),
                (gchar *)gtk_entry_get_text(GTK_ENTRY( ((JanelaPrincipal *)data)  -> texto)) );
}

Analyze the function botao_clicado() and the g_signal_connect(), is among them that occurs the "communication" of widgets.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.