Ffff
Ffff

Reputation: 73

GTK 3 How to connect one signal to multiple widgets?

I need to create a form with the model created by the code bellow, composed by a window with two text entries and one button.

I need to put some text in the entries, and when the button is pressed, put the text gotten in the two entries in an array, (or print them both, or any action with both at the same time)

The code used to create the window is the following:

#include <iostream>
#include <gtk/gtk.h>
using namespace std;

GtkWidget *wventana;
GtkWidget *wgrid;

void ventana(string titulo, int margen)
{
    const char * tituloc = titulo.c_str();
    wventana = gtk_window_new (GTK_WINDOW_TOPLEVEL);
}

void grid()
{
    wgrid = gtk_grid_new();
    gtk_container_add(GTK_CONTAINER(wventana), wgrid);
}

void boton(string texto, int x, int y, int lx, int ly)
{
    const char * wtexto = texto.c_str();

    GtkWidget *wboton;
    wboton = gtk_button_new_with_label (wtexto);
    gtk_grid_attach (GTK_GRID (wgrid), wboton, x, y, lx, ly);
}

void entrada(int x, int y, int lx, int ly)
{
    GtkWidget *wentrada;
    wentrada = gtk_entry_new();
    gtk_grid_attach (GTK_GRID (wgrid), wentrada, x, y, lx, ly);
}

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

    ventana("ventana",10);
    grid();
    entrada(2, 1, 1, 1);
    entrada(2, 2, 1, 1);
    boton("Procesar", 2, 3, 1, 1);

    gtk_widget_show_all (wventana);
    gtk_main ();

    return 0;
}

Please, could someone clarify how can this be made (is not necessary to use the code provided, its only for reference on the results needed)

Upvotes: 0

Views: 1730

Answers (1)

ptomato
ptomato

Reputation: 57870

The best way to do this is to create a structure that you pass as user_data to your signal handlers:

typedef struct {
    GtkWidget *entrada1, *entrada2;
} Widgets;

...

// in main():
Widgets *w = g_slice_new0 (Widgets);
w->entrada1 = entrada (2, 1, 1, 1); // remember to return the widget from entrada()!
w->entrada2 = entrada (2, 2, 1, 1);
GtkWidget *procesar = boton ("Procesar", 2, 3, 1, 1);
g_signal_connect(procesar, "clicked", G_CALLBACK (on_procesar), w);
// ...
gtk_main ();
g_slice_free (Widgets, w);

...

void
on_procesar (GtkButton *procesar, Widgets *w)
{
    // do something with w->entrada1 and w->entrada2
}

Upvotes: 1

Related Questions