I'm only starting to learn GTK3, but I'm having a real struggle. All of the documentation that i could find is for GTK2 and the gnome dev web site is simply not good enough. Now I am trying to get the selected text/item from a GtkComboBox, here is some of the code which I have tried.
gint array =gtk_combo_box_get_active(cbox_quaity);
//or
gchar *array =gtk_combo_box_get_active(cbox_quaity);
If i use the gint, the printout will be "0" (zero) no matter what itom of the list I select. If I use gchar, I get back "(null)". In addition, I get these warnings while compiling.
gcc -c -g -O0 -Wall -pthread -pipe src/main.c `pkg-config --cflags --libs gtk+-3.0` -o main.o
src/main.c: In function ‘btn_start_click’:
src/main.c:41:17: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
gchar *array =gtk_combo_box_get_active(cbox_quaity);
^~~~~~~~~~~~~~~~~~~~~~~~
gcc -o tstreamer-gtk main.o -pthread `pkg-config --cflags --libs gtk+-3.0` -export-dynamic
The user interface was created in glade and the combo box used a GtkListStore. Also, here is the source code of the C code:
#include <gtk/gtk.h>
#include <stdio.h>
GtkWidget *btn_start;
GtkComboBox *cbox_quaity;
#define MAX_WORD_SIZE 32
void btn_start_click(){
gint active_item = gtk_combo_box_get_active(cbox_quaity);
printf("%d\n",active_item);
}
int main(int argc, char *argv[]){
GtkBuilder *builder;
GtkWidget *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
gtk_builder_connect_signals(builder, NULL);
btn_start = GTK_WIDGET(gtk_builder_get_object(builder, "btn_start"));
g_object_unref(builder);
gtk_widget_show(window);
gtk_main();
return 0;
}
// called when window is closed
void on_window_main_destroy(){
gtk_main_quit();
}
This is the "right way to do it" ™:
Yes, you must use an int, not a pointer to int, as explained in the documentation of gtk_combo_box_get_active.
If that doesn't work, this is because you're not calling it at the right moment or place, and hence we need more context to determine the problem.