LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-19-2009, 11:58 PM   #1
gktandc
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Rep: Reputation: 0
how to create a table using gtk?


At first, I'm sorry for my poor English.

I want to create a table to display the data in a mysql's table.
The "Dev Help" tells that the GtkTreeView can make it. I tried it, but I runned into a problem. I donnot know how to add items to the list.

I used this
Code:
void add_to_list(GtkWidget *list, const gchar *str, int COL) {
    GtkListStore *store;
    GtkTreeIter iter;

    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, COL, str, -1);

}
then i got this ( in the attachment)

I looked up the 'Dev Help' again, and I got this method
Code:
gtk_list_store_set_valuesv
But it need a GValue...

I need your help...

Any help will be apreciated.
Attached Images
File Type: png 2009-04-20-125557_185x212_scrot.png (4.3 KB, 7 views)

Last edited by gktandc; 04-20-2009 at 12:01 AM.
 
Old 04-22-2009, 06:54 PM   #2
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
Hello, maybe try this, i found this example that seems to be similar to your problem:

[code]

#include <gtk/gtk.h>
enum
{
BUY_IT = 0,
QUANTITY,
PRODUCT,
COLUMNS
};

typedef struct
{
gboolean buy;
gint quantity;
gchar *product;
} GroceryItem;

const GroceryItem list[] =
{
{ TRUE, 1, "Paper Towels" },
{ TRUE, 2, "Bread" },
{ FALSE, 1, "Butter" },
{ TRUE, 1, "Milk" },
{ FALSE, 3, "Chips" },
{ TRUE, 4, "Soda" },
{ FALSE, 0, NULL }
};

static void setup_tree_view (GtkWidget*);
int main (int argc,

char *argv[])
{
GtkWidget *window, *treeview, *scrolled_win;
GtkListStore *store;
GtkTreeIter iter;
guint i = 0;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Grocery List");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (window, 250, 175);
treeview = gtk_tree_view_new ();
setup_tree_view (treeview);
/* Create a new tree model with three columns, as string, gint and guint. */
store = gtk_list_store_new (COLUMNS, G_TYPE_BOOLEAN, G_TYPE_INT, G_TYPE_STRING);
/* Add all of the products to the GtkListStore. */
while (list[i].product != NULL)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, BUY_IT, list[i].buy,
QUANTITY, list[i].quantity, PRODUCT, list[i].product, -1);
i++;
}

/* Add the tree model to the tree view and unreference it so that the model will
* be destroyed along with the tree view. */
gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store));
g_object_unref (store);
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (scrolled_win), treeview);
gtk_container_add (GTK_CONTAINER (window), scrolled_win);
gtk_widget_show_all (window);
gtk_main ();

return 0;
}

/* Add three columns to the GtkTreeView. All three of the columns will be
* displayed as text, although one is a gboolean value and another is
* an integer. */
static void setup_tree_view (GtkWidget *treeview)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
/* Create a new GtkCellRendererText, add it to the tree view column and
* append the column to the tree view. */
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Buy", renderer, "text", BUY_IT, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Count", renderer, "text", QUANTITY, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Product", renderer, "text", PRODUCT, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
}

[\code]
 
Old 04-22-2009, 06:58 PM   #3
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
I'm sorry, I don't know how to post code.
 
Old 04-23-2009, 12:36 AM   #4
gktandc
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by kike_coello View Post
I'm sorry, I don't know how to post code.
Thank you!
Great job!
I can copy it to my netbeans...
good code.

thanks.
 
Old 04-23-2009, 06:28 AM   #5
jisjis
LQ Newbie
 
Registered: Apr 2009
Posts: 18

Rep: Reputation: 0
Quote:
Originally Posted by kike_coello View Post
Hello, maybe try this, i found this example that seems to be similar to your problem:

[code]

#include <gtk/gtk.h>
enum
{
BUY_IT = 0,
QUANTITY,
PRODUCT,
COLUMNS
};

typedef struct
{
gboolean buy;
gint quantity;
gchar *product;
} GroceryItem;

const GroceryItem list[] =
{
{ TRUE, 1, "Paper Towels" },
{ TRUE, 2, "Bread" },
{ FALSE, 1, "Butter" },
{ TRUE, 1, "Milk" },
{ FALSE, 3, "Chips" },
{ TRUE, 4, "Soda" },
{ FALSE, 0, NULL }
};

static void setup_tree_view (GtkWidget*);
int main (int argc,

char *argv[])
{
GtkWidget *window, *treeview, *scrolled_win;
GtkListStore *store;
GtkTreeIter iter;
guint i = 0;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Grocery List");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (window, 250, 175);
treeview = gtk_tree_view_new ();
setup_tree_view (treeview);
/* Create a new tree model with three columns, as string, gint and guint. */
store = gtk_list_store_new (COLUMNS, G_TYPE_BOOLEAN, G_TYPE_INT, G_TYPE_STRING);
/* Add all of the products to the GtkListStore. */
while (list[i].product != NULL)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, BUY_IT, list[i].buy,
QUANTITY, list[i].quantity, PRODUCT, list[i].product, -1);
i++;
}

/* Add the tree model to the tree view and unreference it so that the model will
* be destroyed along with the tree view. */
gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store));
g_object_unref (store);
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (scrolled_win), treeview);
gtk_container_add (GTK_CONTAINER (window), scrolled_win);
gtk_widget_show_all (window);
gtk_main ();

return 0;
}

/* Add three columns to the GtkTreeView. All three of the columns will be
* displayed as text, although one is a gboolean value and another is
* an integer. */
static void setup_tree_view (GtkWidget *treeview)
{
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
/* Create a new GtkCellRendererText, add it to the tree view column and
* append the column to the tree view. */
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Buy", renderer, "text", BUY_IT, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Count", renderer, "text", QUANTITY, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Product", renderer, "text", PRODUCT, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
}

[\code]


Wow ... Now thats what I call "going out of your way to help someone!"

Rock On mate!


Linux

Last edited by jisjis; 04-26-2009 at 04:43 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you control size and placement of widgets in table under gtk davimint Programming 1 09-10-2008 12:48 PM
Manually create partition table? FogSwimmer Linux - Software 7 12-29-2005 06:28 PM
create table in postgres database kranti Linux - Newbie 1 11-15-2005 06:52 PM
how to create ip rule table? yenonn Linux - Networking 6 07-25-2005 11:57 PM
gtk: table manipulation? jpollack Programming 2 01-27-2005 09:45 PM


All times are GMT -5. The time now is 04:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration