LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-26-2006, 01:59 PM   #1
BackwardsDown
Member
 
Registered: Nov 2005
Posts: 66

Rep: Reputation: 15
string to gchar*


Language: C++
IDE: Kdevelop
Compiler: Gcc
Includes: <gtk/gtk.h> and <string.h>
Distro: Kubuntu Dapper Drake

Hi, I have a question regarding the gtk+ library. Beneath here you will see 2 questions (you will notice they are pretty much the same), I would be very pleased if you could give me the answer to one of them.

1. I want to make a button, the name of a button is in a file called example.txt. How can I do that?

To make a button in gtk, you can do this:
Quote:
GtkWidget *button;
gchar *name;
button = gtk_button_new_with_label (name);
To read a line from the file, you can do this:
Quote:
string line;
ifstream myfile ("example.txt");
getline (myfile,line);
But I cant do this:
Quote:
string line;
ifstream myfile ("example.txt");
getline (myfile,line);
GtkWidget *button;
button = gtk_button_new_with_label (line);
Because gtk+ wants me to insert a gchar* in the function instead of a string.

2. So, how can I convert a gchar* into a string?

Last edited by BackwardsDown; 09-26-2006 at 02:00 PM.
 
Old 09-26-2006, 02:02 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
will this work
Code:
button = gtk_button_new_with_label (line.c_str());
 
Old 09-26-2006, 02:09 PM   #3
BackwardsDown
Member
 
Registered: Nov 2005
Posts: 66

Original Poster
Rep: Reputation: 15
WooT!

Thank you very much (also for the damn fast reaction)

I think I am going to study those functions now

Last edited by BackwardsDown; 09-26-2006 at 02:11 PM.
 
Old 09-26-2006, 02:13 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
no problem
 
Old 09-27-2006, 03:02 PM   #5
BackwardsDown
Member
 
Registered: Nov 2005
Posts: 66

Original Poster
Rep: Reputation: 15
Next problem:

A button needs a callback function, I can set a callback with this:
(1: the button itself
2: the event
3: what callback needs to be called when the event happens
4: the data that is being send to the function)
Quote:
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback), (gpointer) "button 1"
When the button is "clicked", this callback will be called:
Quote:
static void callback( GtkWidget *widget,
gpointer data )
{
g_print ("%s was pressed\n", (gchar *) data);
}
But now I cant seem to find out how I can pass a string to my callback (so instead of "button 1";

Last edited by BackwardsDown; 09-27-2006 at 03:06 PM.
 
Old 09-28-2006, 06:51 PM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
im not sure i understand but here is a stab..

Code:
std::string buttonLabel = "button 1";

..
..

g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback), (gpointer) buttonLabel.c_str());
..
is that what you are talking about?
 
Old 09-28-2006, 06:58 PM   #7
kryptonite0110
Member
 
Registered: Mar 2005
Posts: 59

Rep: Reputation: 15
isn't "gpointer" synonymous to "void *"? If that is the case, you should be able to
do
gchar *str = "hello";
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback), (gpointer) str);
then
static void callback(GtkWidget *w, gpointer d) {
g_print("\"%s\" was passed\n", (gchar *) d);
}

you may try to create another button to modify the value of str, or even do so in
the above function like this:

static void callback(GtkWidget *w, gpointer d) {
g_print("\"%s\" was passed\n", (gchar *) d);
((gchar *) d)[0]++;
}
this is, of course, only useful in a proven concept environment, and i'm really not an expert in the gtk programming environment, but that should do the trick
 
Old 09-28-2006, 07:17 PM   #8
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
kryptonite0110,

yeh im not much of a gtk programmer either..

in the above postings OP was using std::string, so thats why i was throwing that out the way i did.
 
  


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
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
C String Help Scrag Programming 6 04-14-2004 12:47 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Gimp Tool Kit gchar Widget comparison... chewysplace Programming 2 01-30-2004 04:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration