How can I send data through a GTK callback? I've Googled, and with the information I found created this:
#include <gtk/gtk.h>
#include <stdio.h>
void button_clicked( GtkWidget *widget, GdkEvent *event, gchar *data);
int main( int argc, char *argv[]){
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
button = gtk_button_new_with_label("Go!");
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(button_clicked),"test" );
gtk_widget_show(window);
gtk_widget_show(button);
gtk_main();
return 0;
}
void button_clicked( GtkWidget *widget, GdkEvent *event, gchar *data){
printf("%s \n", (gchar *) data);
return;
}
But it just Segfaults when I press the button. What is the right way to do this?
It segfaults because "clicked" doesn't have a GdkEvent parameter. If you remove the second argument in button_clicked() it should work.
Install Devhelp application from where you can easily browse GTK+ and GNOME documentation, including signal definitions.
Related
I am programming on linux program and I am trying to prepare program with GTK+3, but I don't know how close program automatically after execute the function without kill that execute.
the step that i want in my program is:
open gtk window and this window there is button (open Xreader).
press button, it tells system to execute xreader program.
close gtk window automatically without needing to close xreader .
here is my code
#include<stdio.h>
#include<gtk/gtk.h>
static void
open_app(GtkWidget *Widget, gpointer data){
system("xreader");
gtk_main_quit();
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *button1;
gtk_init(&argc, &argv);
/*make window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/*give name to the window*/
gtk_window_set_title(GTK_WINDOW(window), "launcher");
/*make size of window*/
gtk_window_set_default_size(GTK_WINDOW(window), 700, 700);
/*open the window in the meddal*/
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
button1 = gtk_button_new_with_label("Open Xreader");
gtk_container_add(GTK_CONTAINER(window), button1);
gtk_widget_show_all(window);
g_signal_connect(button1,"clicked",G_CALLBACK(open_app), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
everytime trying to execute my program and press the button (open Xreader) gtk not close till close xreader .
You cannot use system here, because according to its documentation, https://man7.org/linux/man-pages/man3/system.3.html
system() returns after the command has been completed.
Since you are using gtk, don't forget library functions provided by glib. It has a bunch of functions related to process and thread handling. For example, you can use GSubprocess class from glib, g_subprocess_new.
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
static void open_app(GtkWidget *Widget, gpointer data) {
g_subprocess_new(G_SUBPROCESS_FLAGS_NONE, NULL, "xreader", NULL);
gtk_main_quit();
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *button1;
gtk_init(&argc, &argv);
/*make window */
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
/*give name to the window*/
gtk_window_set_title(GTK_WINDOW(window), "launcher");
/*make size of window*/
gtk_window_set_default_size(GTK_WINDOW(window), 700, 700);
/*open the window in the meddal*/
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
button1 = gtk_button_new_with_label("Open Xreader");
gtk_container_add(GTK_CONTAINER(window), button1);
gtk_widget_show_all(window);
g_signal_connect(button1, "clicked", G_CALLBACK(open_app), NULL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
Consider the following code:
#include <gtk/gtk.h>
static void stop(GtkWidget *window, GdkEventKey *key, gboolean *key_held)
{
*key_held = FALSE;
g_print("stopped!\n");
}
static void counter(GtkWidget *window, GdkEventKey *key, gpointer user_data)
{
gboolean key_held = TRUE;
gulong signal_ID = g_signal_connect(window, "key-release-event", G_CALLBACK(stop), &key_held);
for (unsigned long int i = 0;key_held;i++)
{
g_print("%li\n", i);
}
g_signal_handler_disconnect(window, signal_ID);
}
int main(int argc, char **argv)
{
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_add_events(window, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(window, "key-press-event", G_CALLBACK(counter), NULL);
gtk_widget_show(window);
gtk_main();
}
It's simple: the counter starts to count from 0 when the user presses a key and continues to count until the user release the key...
Or that's at least how it's supposed to work. What actually happens is that when a user presses a key, the counter starts and doesn't stop when the user release the key. The only way to stop counting is to terminate the program. Also, not only the "key-release-event" is not triggered but "delete-event" isn't too: I have to press Ctrl+C to terminate the program.
In the following example, compiled with GTK3, GtkExpander collapses unintendedly when I click in the entry field.
#include <gtk/gtk.h>
static void destroy (GtkWidget *widget, gpointer data)
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
gtk_init (&argc, &argv);
GtkWidget *entry;
entry = gtk_entry_new ();
GtkWidget *expander;
expander = gtk_expander_new ("test");
gtk_expander_set_expanded (GTK_EXPANDER(expander), TRUE);
gtk_container_add (GTK_CONTAINER(expander), entry);
GtkWidget *window;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_add (GTK_CONTAINER(window), expander);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
However, GtkExpander does not collapse if it is expanded after gtk_widget_show_all(), i.e.:
gtk_widget_show_all (window);
gtk_expander_set_expanded (GTK_EXPANDER(expander), TRUE);
What's wrong with expanding the widget before gtk_widget_show_all()?
This was a bug in GTK+ which is fixed now.
For details see:
https://bugzilla.gnome.org/show_bug.cgi?id=783145
https://bugzilla.gnome.org/show_bug.cgi?id=774134
This code make a program that create a window with label, if you click the label, the program executes an fprintf of buffer passed as an argument of g_signal_connect(G_OBJECT(eventbox), "button_press_event", G_CALLBACK(on_event_clicked), buffer). Previously the program put in buffer the string "Hello Word" and then the program should print this message, but maybe the program print only garbled char. Where I'm wrong?
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
void on_event_clicked (GtkWidget* widget, gpointer user_data);
int main (int argc, char **argv) {
GtkWidget *window;
GtkWidget *eventbox;
GtkWidget *label;
char* buffer = malloc(sizeof(char)*10);
strcpy(buffer, "Hello Word\0");
gtk_init (&argc,&argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
label = gtk_label_new ("Hello Word");
eventbox = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER(eventbox), label);
gtk_container_add (GTK_CONTAINER(window), eventbox);
gtk_widget_show_all (window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(eventbox), "button_press_event",
G_CALLBACK(on_event_clicked), buffer);
gtk_main();
return 0;
}
void on_event_clicked (GtkWidget *widget, gpointer user_data) {
char* pn = user_data;
fprintf(stderr, "%s\n", pn);
}
Your prototype for on_event_clicked() is wrong, it doesn't match what GTK+ expects.
It should be:
gboolean user_function (GtkWidget *widget, GdkEvent *event, gpointer user_data);
You need to add the missing argument to your function, and also deal with the requirement for a return value. Remember to read the signal documentation seriously.
I'm trying to pass an object to the callback of a "clicked" event in order to set the text of the target label.
Here's my code so far:
#include <gtk/gtk.h>
typedef struct {
int i;
GtkWidget *target;
} Data;
void change( GtkWidget *widget,
Data *data )
{
gtk_label_set_text(GTK_LABEL(data->target), "it did!");
}
int main( int argc,
char* argv[] )
{
gtk_init(&argc, &argv);
GtkWidget *window, *label, *button;
Data data;
data.i = 0;
data.target = label;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GTKdemo");
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
GtkGrid *grid = gtk_grid_new();
button = gtk_button_new_with_label("Click here");
g_signal_connect(button, "clicked", G_CALLBACK(change), &data);
gtk_grid_attach(grid, button, 0,0,1,1);
label = gtk_label_new("this will change");
gtk_grid_attach(grid, label, 0,1,1,1);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(grid));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
I'm trying to set the text of label when button is clicked.
I tried gtk callback multiple arguments but no dice. Help?
Okay, I fixed it by moving data.target = label to execute after the g_signal_connect of the button.
Perhaps the problem arose from using the pointer address before the object variables were defined.