GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 02, 2014 2:25 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: gstring utf-8 problem
PostPosted: Tue Dec 25, 2007 10:44 pm 
Offline
Familiar Face

Joined: Mon Dec 10, 2007 10:57 pm
Posts: 9
Hello,

if i write:
gtk_list_store_set (store, &iter, PRODUCT, "myText", -1);

everything is fine.

if i write:
GString *tempString;
tempString = g_string_new("myText");
gtk_list_store_set (store, &iter, PRODUCT, tempString, -1);

i get some "invalid utf-8 encoded text"....

Could someone help pls?

Roland


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 1:08 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
What type is PRODUCT? If it's a G_TYPE_STRING then it needs a gchar* not a GString type. To store a GString, I think you would use G_TYPE_OBJECT.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 9:53 am 
Offline
Familiar Face

Joined: Mon Dec 10, 2007 10:57 pm
Posts: 9
Hmm, thx.
Crashed when i used G_TYPE_OBJECT and GString (but didn't investigate further why).

But
gtk_list_store_set (store, &iter, PRODUCT, tempString->str, -1);
works.
Is there any other 'conversion' GString to gchar* or is this approach correct?

Thx,

Roland


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 1:51 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
That works, however, by storing it with the str member, you're only storing the gchar* of the GString object, not the entire object. Therefore, doing so is the same as:

Code:
gchar *tempString = "MyText";
gtk_list_store_set (store, &iter, PRODUCT, tempString, -1);


Furthermore, if you're not then calling g_string_free on then you'll end up with a memory leak. Can you paste your demo code? I really think you need to save it as a GObject.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 2:11 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Oh... but you won't be able to display the gstring in the list view (GtkTreeView) as an object. So you're probably actually better off just using a gchar*.

Either that or storing the GString as a G_TYPE_OBJECT which is not displayed as well as the gchar* (->str) portion which could be displayed.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 2:39 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Let me see if I can clarify my thoughts...

A GString is a GObject. The G_TYPE_STRING refers to a "string" in the standard C style... a gchar pointer (gchar*).

The "model" is the GtkListStore. You can store just about anything you want in this model. Come time to display data from your model using something like a GtkTreeView, the data in the position you're trying to display will be rendered by a "cell renderer" of some sort. If you intend to use a GtkCellRendererText, it works with data type of (gchar*), not a GString. Although you can still store a GString in your "model" as an object, in order to display it using your "view", you'll need a renderer that supports it.

In short, unless there's a good reason for needing to store the whole GString object, it's probably a better idea to simply store gchar* instead. If you really need it as a GString when manipulating it, you can simply copy the (->str) member to a newly allocated gchar* and save it, and then create a new GString from that value when you get it from the list store.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 2:40 pm 
Offline
Familiar Face

Joined: Mon Dec 10, 2007 10:57 pm
Posts: 9
Hello again,

this is the code - works with G_TYPE_STRING and tempString->str but not with G_TYPE_OBJECT and tempStr:

#include <gtk/gtk.h>

enum
{
QUANTITY,
PRODUCT,
COLUMNS
};

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

const GroceryItem list[] =
{
{ 1, "Paper Towels" },
{ 2, "Bread" },
{ 1, "Butter" },
{ 1, "Milk" },
{ 3, "Chips" },
{ 4, "Soda" },
{ 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;
GString *tempString;

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);

g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);

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_INT, G_TYPE_STRING);

/* Add all of the products to the GtkListStore. */
while (list[i].product != NULL)
{
gtk_list_store_append (store, &iter);
tempString = g_string_new("");
tempString = g_string_append(tempString, "<span foreground=\"blue\" size=\"x-large\" style=\"italic\">");
tempString = g_string_append(tempString, list[i].product);
tempString = g_string_append(tempString, "</span>");
gtk_list_store_set (store, &iter, QUANTITY, list[i].quantity, PRODUCT, tempString->str, -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
("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, "markup", PRODUCT, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 9:19 pm 
Offline
Familiar Face

Joined: Mon Dec 10, 2007 10:57 pm
Posts: 9
Aaah - thank you for your explanations.
It's a lot clearer for me now.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Exabot [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group