1

I have the following problem. I'm developing an application which uses gtktreeview for displaying data retrieved from MySQL database.

I've already done few examples of gtktreeview, I was able to add, clear, remove from the list. Now I wanted to adapt this example to my application and suddenly it doesn't work anymore.

Here is the example I used which works.

The enum:

    static enum {
        FIRST_NAME,
        LAST_NAME,
        N_COL
    };

The variables:

    static GtkWidget *list;
    static GtkWidget *f_entry;
    static GtkWidget *l_entry;

The setup of a list:

    static GtkWidget *setup_list()
    {
        GtkWidget *sc_win;
        GtkListStore *store;
        GtkCellRenderer *cell;
        GtkTreeViewColumn *column;

        sc_win = gtk_scrolled_window_new(NULL, NULL);
        gtk_widget_set_usize(sc_win, 250, 150);
        store = gtk_list_store_new(N_COL, G_TYPE_STRING, G_TYPE_STRING);
        list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
        cell = gtk_cell_renderer_text_new();

        // column of the names
        column = gtk_tree_view_column_new_with_attributes("Imię", cell, "text", FIRST_NAME, NULL);
        gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

        // column of the surnames
        column = gtk_tree_view_column_new_with_attributes("Nazwisko", cell, "text", LAST_NAME, NULL);
        gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

        // scrolls
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sc_win), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
        gtk_container_add(GTK_CONTAINER(sc_win), list);

        // free the store variable and return the scrolled window pointer
        g_object_unref(G_OBJECT(store));
        return sc_win;
    }

The function which adds an element:

    static void list_add_cb(GtkWidget* widget, gpointer data)
    {
        GtkListStore *store;
        GtkTreeIter iter;
        const char *first;
        const char *last;

        first = gtk_entry_get_text(GTK_ENTRY(f_entry));
        last = gtk_entry_get_text(GTK_ENTRY(l_entry));
        store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));

        gtk_list_store_append(store, &iter);
        gtk_list_store_set(store, &iter, FIRST_NAME, first, LAST_NAME, last, -1);
    }

And here is the equivalent in my application.

The enum:

    enum {
        ID_ALIAS,
        N_COL
    };

The variables (only those significant for the example):

    static GtkWidget *list_pas;
    static GtkWidget *list_key;

The setup of a list (almost identycle, except of passing the column name in the parameter and usage of only one column):

    static GtkWidget *setup_list(char *typ)
    {
        GtkWidget *sc_win;
        GtkWidget *list;
        GtkListStore *store;
        GtkCellRenderer *cell;
        GtkTreeViewColumn *column;

        sc_win = gtk_scrolled_window_new(NULL, NULL);
        gtk_widget_set_usize(sc_win, 250, 150);
        store = gtk_list_store_new(N_COL, G_TYPE_STRING, G_TYPE_STRING);
        list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
        cell = gtk_cell_renderer_text_new();

        column = gtk_tree_view_column_new_with_attributes(typ, cell, "text", ID_ALIAS, NULL);
        gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

        // scrolls
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sc_win), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
        gtk_container_add(GTK_CONTAINER(sc_win), list);

        // free the store variable and return the scrolled window pointer
        g_object_unref(G_OBJECT(store));
        return sc_win;
    }

The function which adds an element (in data there is a string of the element to add to the list and in the widget there is scrolled window which contains proper list):

    static void list_add_cb(GtkWidget* widget, gpointer data)
    {
        GtkListStore *store;
        GtkTreeIter iter;
        store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(widget)));

        gtk_list_store_append(store, &iter);
        gtk_list_store_set(store, &iter, ID_ALIAS, (char *) data*, -1);
    }

It does not work because of following errors I get everytime MySQL function tryes to write the data in their fields:

    (app_0:3642): GLib-GObject-WARNING **: invalid cast from `GtkScrolledWindow' to `GtkTreeView'

    (app_0:3642): Gtk-CRITICAL **: IA__gtk_tree_view_get_model: assertion `GTK_IS_TREE_VIEW (tree_view)' failed

    (app_0:3642): Gtk-CRITICAL **: IA__gtk_list_store_append: assertion `GTK_IS_LIST_STORE (list_store)' failed

    (app_0:3642): Gtk-CRITICAL **: IA__gtk_list_store_set_valist: assertion `GTK_IS_LIST_STORE (list_store)' failed

Here is how the function of adding a row is called:

    list_add_cb(list, row[i] ? row[i] : "NULL");

I will also say that when I printf the row[i] ? row[i] : "NULL" I get correct value.

The MySQL part of my application works more than fine. The only warning I get while compilation is useless class storage specifier in empty declaration which I researched and here on stackoverflow.com I have learned this has nothing to do with this problem.

The other difference is I'm creating mutliple lists in my application. To verify if this was the source of the problem I have deleted all the lists except one single list and this changed nothing.

I've been also trying to charge the list with input fields and a button trigger, but this hasn't change anything. I've also tryed reordering declarations of the variables with the enum this had none effect on the result neither.

I haven't found anything usefull, I'm out of ideas, please help. I've been trying to explain this as clearly as possible, please ask question and correct me and my post. Thanks.

Marek
  • 1,413
  • 2
  • 20
  • 36
  • The first warning is the key. No matter what you think `list` is: you have a `GtkScrolledWindow` in it instead of a `GtkTreeView`. The error is in the code you did not post. – ntd Jun 08 '13 at 06:57
  • Precisely, it is based on this tutorial http://www.youtube.com/watch?v=4z_XdP3uxB8, in this example it is also a GtkScrolledWindow and works perfectly. I've verified it myself. I didn't posted all the code because the rest is MySQL and creating window, buttons etc. – Marek Jun 08 '13 at 09:08
  • ntd, you were right, I've been mistaken, but it wasn't all, I will explain in the answer – Marek Jun 09 '13 at 15:29

1 Answers1

0

I've found the solution. Unfortunately I don't know exactly why it solves the problem.

As user ntd said in the comment I used GtkScrolledWindow to append lists, I though it's ok because it was this way in the example, but in the example there were secondary variables, not global in the main function which I didn't noticed. So thank you ntd for your point.

Second thing came of using multiple lists. The function static GtkWidget *setup_list(char *typ) had to be doubled. The funny thing was that I couldn't pass the gtktreeview object in the argument of this function, but once I've created two functions setup_list(char *typ) and setup_list2(char *typ) where the only difference was a variable used for storing gtktreeview.

I don't really know why it does work this way, I'll be happy to learn if someone has an idea. I'm happy anyway because my project can go further now. Cheers people!

Marek
  • 1,413
  • 2
  • 20
  • 36