By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

(gtk+) button with integer as its label

P: n/a
Hello!

I'm really new to c++. I've been doing java for a while though.

I'm using linux so I wanted to make a gtk+ gui application. I want to
make a simple calculator to help me learn, so, say I want three
buttons with labels "1", "2" and "3", how can I change the following
code to work:

GtkWidget *button;

int i;
for (i = 0; i<3; i++);
{
button = gtk_button_new_with_label (i);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
(callback), (gpointer) "button 2");
gtk_table_attach_defaults (GTK_TABLE (table), button, i, (i+1), 1,
2);
gtk_widget_show (button);
}

thanks!!!!!

Jun 2 '07 #1
Share this Question
Share on Google+
19 Replies


P: n/a
stinkinrich88 wrote:
Hello!

I'm really new to c++. I've been doing java for a while though.

I'm using linux so I wanted to make a gtk+ gui application. I want to
make a simple calculator to help me learn, so, say I want three
buttons with labels "1", "2" and "3", how can I change the following
code to work:

GtkWidget *button;

int i;
for (i = 0; i<3; i++);
{
button = gtk_button_new_with_label (i);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
(callback), (gpointer) "button 2");
gtk_table_attach_defaults (GTK_TABLE (table), button, i, (i+1), 1,
2);
gtk_widget_show (button);
}

thanks!!!!!
In what way doesn't it work?

In Java, Swing is part of the language (as good as). In C++, GTK+ or any
other windowing toolkit are not part of the language. For better or
worse they are 3rd party. This group deals with C++ language questions
only, GTK+ questions are off topic.

Assuming your question is about GTK+ (it's hard to tell because you
don't actually say what is wrong), then you would be better asking in a
Linux development group, news:comp.os.linux.development.apps perhaps.

john
Jun 2 '07 #2

P: n/a
John Harrison wrote:
stinkinrich88 wrote:
>Hello!

I'm really new to c++. I've been doing java for a while though.

I'm using linux so I wanted to make a gtk+ gui application. I want to
make a simple calculator to help me learn, so, say I want three
buttons with labels "1", "2" and "3", how can I change the following
code to work:

GtkWidget *button;

int i;
for (i = 0; i<3; i++);
{
button = gtk_button_new_with_label (i);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
(callback), (gpointer) "button 2");
gtk_table_attach_defaults (GTK_TABLE (table), button, i,
(i+1), 1,
2);
gtk_widget_show (button);
}

thanks!!!!!

In what way doesn't it work?
A little bit of guesswork and I'd say your problem is that you don't
know how to convert an integer to a string in C++. Is that correct? If
so that's a perfectly good C++ question.

I would do it this way, though no doubt I'll get criticism for it

#include <stdio.h>

int i = ...;
char str[99];
sprintf(str, "%d", i);

john
Jun 2 '07 #3

P: n/a
On Jun 2, 1:18 pm, John Harrison <john_androni...@hotmail.comwrote:
John Harrison wrote:
stinkinrich88 wrote:
Hello!
I'm really new to c++. I've been doing java for a while though.
I'm using linux so I wanted to make a gtk+ gui application. I want to
make a simple calculator to help me learn, so, say I want three
buttons with labels "1", "2" and "3", how can I change the following
code to work:
GtkWidget *button;
int i;
for (i = 0; i<3; i++);
{
button = gtk_button_new_with_label (i);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
(callback), (gpointer) "button 2");
gtk_table_attach_defaults (GTK_TABLE (table), button, i,
(i+1), 1,
2);
gtk_widget_show (button);
}
thanks!!!!!
In what way doesn't it work?

A little bit of guesswork and I'd say your problem is that you don't
know how to convert an integer to a string in C++. Is that correct? If
so that's a perfectly good C++ question.

I would do it this way, though no doubt I'll get criticism for it

#include <stdio.h>

int i = ...;
char str[99];
sprintf(str, "%d", i);

john
Hi John, thanks for the help, that's cleared a bit up for me. I
thought it would be a problem converting an integer to a string but it
turns out it wants some other weird format. Here's the error when I
use your code:

invalid conversion from 'int' to 'const gchar*'

Anyway, thanks for the help, i'll take my problem to another group. I
tried searching for gtk+ before I tried "c++" with little luck. So
I'll give a linux forum a go!

Thanks!

Jun 2 '07 #4

P: n/a
On Jun 2, 1:36 pm, stinkinrich88 <stinkinric...@googlemail.comwrote:
On Jun 2, 1:18 pm, John Harrison <john_androni...@hotmail.comwrote:
John Harrison wrote:
stinkinrich88 wrote:
>Hello!
>I'm really new to c++. I've been doing java for a while though.
>I'm using linux so I wanted to make a gtk+ gui application. I want to
>make a simple calculator to help me learn, so, say I want three
>buttons with labels "1", "2" and "3", how can I change the following
>code to work:
> GtkWidget *button;
> int i;
> for (i = 0; i<3; i++);
> {
> button = gtk_button_new_with_label (i);
> g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
>(callback), (gpointer) "button 2");
> gtk_table_attach_defaults (GTK_TABLE (table), button, i,
>(i+1), 1,
>2);
> gtk_widget_show (button);
> }
>thanks!!!!!
In what way doesn't it work?
A little bit of guesswork and I'd say your problem is that you don't
know how to convert an integer to a string in C++. Is that correct? If
so that's a perfectly good C++ question.
I would do it this way, though no doubt I'll get criticism for it
#include <stdio.h>
int i = ...;
char str[99];
sprintf(str, "%d", i);
john

Hi John, thanks for the help, that's cleared a bit up for me. I
thought it would be a problem converting an integer to a string but it
turns out it wants some other weird format. Here's the error when I
use your code:

invalid conversion from 'int' to 'const gchar*'

Anyway, thanks for the help, i'll take my problem to another group. I
tried searching for gtk+ before I tried "c++" with little luck. So
I'll give a linux forum a go!

Thanks!
If anyone has the same trouble, I've asked another group here:
http://groups.google.com/group/comp....d605dcc086004b

Jun 2 '07 #5

P: n/a
stinkinrich88 wrote:
On Jun 2, 1:18 pm, John Harrison <john_androni...@hotmail.comwrote:
>John Harrison wrote:
>>stinkinrich88 wrote:
Hello!
I'm really new to c++. I've been doing java for a while though.
I'm using linux so I wanted to make a gtk+ gui application. I want to
make a simple calculator to help me learn, so, say I want three
buttons with labels "1", "2" and "3", how can I change the following
code to work:
GtkWidget *button;
int i;
for (i = 0; i<3; i++);
{
button = gtk_button_new_with_label (i);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
(callback), (gpointer) "button 2");
gtk_table_attach_defaults (GTK_TABLE (table), button, i,
(i+1), 1,
2);
gtk_widget_show (button);
}
thanks!!!!!
In what way doesn't it work?
A little bit of guesswork and I'd say your problem is that you don't
know how to convert an integer to a string in C++. Is that correct? If
so that's a perfectly good C++ question.

I would do it this way, though no doubt I'll get criticism for it

#include <stdio.h>

int i = ...;
char str[99];
sprintf(str, "%d", i);

john

Hi John, thanks for the help, that's cleared a bit up for me. I
thought it would be a problem converting an integer to a string but it
turns out it wants some other weird format. Here's the error when I
use your code:

invalid conversion from 'int' to 'const gchar*'
I don't think my code would produce such an error, because there is no
gchar in my code. Gchar is something specific to GTK+. Perhaps it is the
way you incorporated my code into your code. For instance, did you
change this

button = gtk_button_new_with_label (i);

to this?

button = gtk_button_new_with_label (str);

john
Jun 2 '07 #6

P: n/a
John Harrison wrote:
char str[99];
sprintf(str, "%d", i);
IMO if you can't give a good reason for the "99" there, that's bad
advice. Why 99? What's wrong with 89. Or 50? Or 35? Or 338 for that
matter? Why precisely 99?

Those kind of completely arbitrary literals feel more like kludges
than anything else. Are kludges really what you want to teach newbies?
Jun 2 '07 #7

P: n/a
stinkinrich88 wrote:
On Jun 2, 1:18 pm, John Harrison <john_androni...@hotmail.comwrote:
>John Harrison wrote:
>>stinkinrich88 wrote:
[snip]
>I would do it this way, though no doubt I'll get criticism for it

#include <stdio.h>

int i = ...;
char str[99];
sprintf(str, "%d", i);

Hi John, thanks for the help, that's cleared a bit up for me. I
thought it would be a problem converting an integer to a string but it
turns out it wants some other weird format. Here's the error when I
use your code:

invalid conversion from 'int' to 'const gchar*'
Yes. I guess it's the line
button = gtk_button_new_with_label (i);
that triggers the error? There should be a line number with the error
message.

What the message is saying is: The function gtk_button_new_with_label
wants a const gchar* (gchar is defined as char) as an argument, but i is
an int. The compiler doesn't know how to convert an int to a const gchar*.

John provided a method of converting an int to a const char*. There are
other ways to do it. In C++ you should generally prefer std::string for
strings. To send a std::string to a function wanting a const char*, you
can call std::string's c_str() member function. To get a std::string
from an int, you can make a conversion function like stringify in the
FAQ:
<URL:http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.1>

--
rbh
Jun 2 '07 #8

P: n/a
Juha Nieminen wrote:
John Harrison wrote:
>char str[99];
sprintf(str, "%d", i);

IMO if you can't give a good reason for the "99" there, that's bad
advice. Why 99?
What's wrong with 89. Or 50? Or 35? Or 338 for that
matter? Why precisely 99?
99 is easier to type, and it's more distinctive than the other
alternatives.
>
Those kind of completely arbitrary literals feel more like kludges
than anything else. Are kludges really what you want to teach newbies?
Frankly I don't care.

john
Jun 2 '07 #9

P: n/a
On Jun 2, 2:21 pm, Robert Bauck Hamar <hamro...@yahoo.nowrote:
stinkinrich88 wrote:
On Jun 2, 1:18 pm, John Harrison <john_androni...@hotmail.comwrote:
John Harrison wrote:
stinkinrich88 wrote:
[snip]
I would do it this way, though no doubt I'll get criticism for it
#include <stdio.h>
int i = ...;
char str[99];
sprintf(str, "%d", i);
Hi John, thanks for the help, that's cleared a bit up for me. I
thought it would be a problem converting an integer to a string but it
turns out it wants some other weird format. Here's the error when I
use your code:
invalid conversion from 'int' to 'const gchar*'

Yes. I guess it's the line
button = gtk_button_new_with_label (i);
that triggers the error? There should be a line number with the error
message.

What the message is saying is: The function gtk_button_new_with_label
wants a const gchar* (gchar is defined as char) as an argument, but i is
an int. The compiler doesn't know how to convert an int to a const gchar*.

John provided a method of converting an int to a const char*. There are
other ways to do it. In C++ you should generally prefer std::string for
strings. To send a std::string to a function wanting a const char*, you
can call std::string's c_str() member function. To get a std::string
from an int, you can make a conversion function like stringify in the
FAQ:
<URL:http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-...>

--
rbh
Doesn't stringify have the same effect as John's idea? If so won't it
still cause the same error? Don't we want a const gchar*, not a char*?

Jun 2 '07 #10

P: n/a
John Harrison wrote:
> Those kind of completely arbitrary literals feel more like kludges
than anything else. Are kludges really what you want to teach newbies?

Frankly I don't care.
If that's so, why bother giving any answer at all?
Jun 2 '07 #11

P: n/a
stinkinrich88 wrote:
Doesn't stringify have the same effect as John's idea? If so won't it
still cause the same error? Don't we want a const gchar*, not a char*?
You can give a char* to a function taking a const gchar*.
Jun 2 '07 #12

P: n/a
On Jun 2, 1:40 pm, John Harrison <john_androni...@hotmail.comwrote:
stinkinrich88 wrote:
On Jun 2, 1:18 pm, John Harrison <john_androni...@hotmail.comwrote:
John Harrison wrote:
stinkinrich88 wrote:
Hello!
I'm really new to c++. I've been doing java for a while though.
I'm using linux so I wanted to make a gtk+ gui application. I want to
make a simple calculator to help me learn, so, say I want three
buttons with labels "1", "2" and "3", how can I change the following
code to work:
GtkWidget *button;
int i;
for (i = 0; i<3; i++);
{
button = gtk_button_new_with_label (i);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK
(callback), (gpointer) "button 2");
gtk_table_attach_defaults (GTK_TABLE (table), button, i,
(i+1), 1,
2);
gtk_widget_show (button);
}
thanks!!!!!
In what way doesn't it work?
A little bit of guesswork and I'd say your problem is that you don't
know how to convert an integer to a string in C++. Is that correct? If
so that's a perfectly good C++ question.
I would do it this way, though no doubt I'll get criticism for it
#include <stdio.h>
int i = ...;
char str[99];
sprintf(str, "%d", i);
john
Hi John, thanks for the help, that's cleared a bit up for me. I
thought it would be a problem converting an integer to a string but it
turns out it wants some other weird format. Here's the error when I
use your code:
invalid conversion from 'int' to 'const gchar*'

I don't think my code would produce such an error, because there is no
gchar in my code. Gchar is something specific to GTK+. Perhaps it is the
way you incorporated my code into your code. For instance, did you
change this

button = gtk_button_new_with_label (i);

to this?

button = gtk_button_new_with_label (str);

john
well shut my mouth! Yeah, I had done it wrong! you're solution works
fine thank you! Though, I might look into stringify again due to the
whole - not being the best practice - thing.

Thanks!!!!! sorry for being thick

Jun 2 '07 #13

P: n/a
On Jun 2, 2:21 pm, Robert Bauck Hamar <hamro...@yahoo.nowrote:
stinkinrich88 wrote:
On Jun 2, 1:18 pm, John Harrison <john_androni...@hotmail.comwrote:
John Harrison wrote:
stinkinrich88 wrote:
[snip]
I would do it this way, though no doubt I'll get criticism for it
#include <stdio.h>
int i = ...;
char str[99];
sprintf(str, "%d", i);
Hi John, thanks for the help, that's cleared a bit up for me. I
thought it would be a problem converting an integer to a string but it
turns out it wants some other weird format. Here's the error when I
use your code:
invalid conversion from 'int' to 'const gchar*'

Yes. I guess it's the line
button = gtk_button_new_with_label (i);
that triggers the error? There should be a line number with the error
message.

What the message is saying is: The function gtk_button_new_with_label
wants a const gchar* (gchar is defined as char) as an argument, but i is
an int. The compiler doesn't know how to convert an int to a const gchar*.

John provided a method of converting an int to a const char*. There are
other ways to do it. In C++ you should generally prefer std::string for
strings. To send a std::string to a function wanting a const char*, you
can call std::string's c_str() member function. To get a std::string
from an int, you can make a conversion function like stringify in the
FAQ:
<URL:http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-...>

--
rbh
thank you very much. stringify(i).c_str() worked a treat!

Jun 2 '07 #14

P: n/a
Juha Nieminen wrote:
John Harrison wrote:
>> Those kind of completely arbitrary literals feel more like kludges
than anything else. Are kludges really what you want to teach newbies?
Frankly I don't care.

If that's so, why bother giving any answer at all?
I don't care about the arbitrariness in my answer, I do care about
giving an answer.

I'm not claiming my way is the best, it's just my way. Others can do as
they please.

john
Jun 2 '07 #15

P: n/a
John Harrison wrote:
I don't care about the arbitrariness in my answer, I do care about
giving an answer.

I'm not claiming my way is the best, it's just my way. Others can do as
they please.
I really don't think that spreading bad programming habits is such
a good idea.
Jun 2 '07 #16

P: n/a
Juha Nieminen wrote:
John Harrison wrote:
>I don't care about the arbitrariness in my answer, I do care about
giving an answer.

I'm not claiming my way is the best, it's just my way. Others can do as
they please.

I really don't think that spreading bad programming habits is such
a good idea.
But I don't think it's a bad habit! There's nothing wrong with it.

john
Jun 2 '07 #17

P: n/a

John Harrison <jo*************@hotmail.comwrote in message...
Juha Nieminen wrote:
John Harrison wrote:
I don't care about the arbitrariness in my answer, I do care about
giving an answer.
I'm not claiming my way is the best, it's just my way. Others can do as
they please.
I really don't think that spreading bad programming habits is such
a good idea.

But I don't think it's a bad habit! There's nothing wrong with it.
john
Hi John,

You seem to be a 'C' programmer in a 'C++' group. <G>
[ <stdio.h== 'C' header. <cstdio== 'C++' header (std::) ]
#include <string>
#include <sstream>

// void SomeFunc( char const *dummy );

int main(){
std::ostringstreaam out;
int Num( 12345 );
out<<Num;
std::string TheNum( out.str() );
SomeFunc( TheNum.c_str() );
// or: SomeFunc( out.str().c_str() ); // without the string

// -- clear the stream if more numbers to convert --
out.clear(); out.str(""); // reset fail-flags and empty stream.
// ....
return 0;
} // main()

Knowing both ways ('C' and 'C++') is a good thing. Then you have a choice.
The FAQ has a nice 'template' to go to/from numbers and strings (the heart
of which I showed above).
class MyThing{/* bunch of data */};

char str[ SIZE ];
MyThing Obj( /*parms*/ );
sprintf( str, "%???", Obj ); // <----

A stream 'operator<<()' overload is easy.
Then:
MyOstream<<Obj;

--
Bob R
POVrookie
Jun 2 '07 #18

P: n/a
BobR wrote:
Knowing both ways ('C' and 'C++') is a good thing. Then you have a choice.
I didn't really object to the "C way" of implementing this solution,
but to using a completely arbitrary, completely unjustified literal
'99' as the size of the buffer. There are many minor problems with
that.

Firstly, why does it have to be so large? An integer, even a 64-bit
one, when printed as ascii decimal representation, takes at most 22
characters (including the final '\0'). So why allocate space for 99,
wasting needless space?

The "hacker" answer to this would probably be: "Yeah, but what if
in some future system integers are larger than 64 bits? Or what if
you want to print a double instead? That might take more space."
And that answer, in fact, reveals another fault in this solution:
Fixed-sized buffers, with all their inherent problems, have been
the pest of C/C++ programming for over 3 decades. Just using larger
(but still fixed-sized) buffers is not really the solution. In a more
complicated situation, when the code has developed for years, that
one "integer" might in fact be changed to something more complex and
longer, and it may just happen that the buffer becomes too small.

"It works well in this simple case" is, IMO, a bad answer because
it just teaches bad habits, especially when this kind of answer is
given to a beginner who is still learning. Even if this solution was
kind of ok in *this* specific situation, the beginner might learn this
as a more *generic* solution which he might start using in a multitude
of other situations, thus perpetuating the eternal fixed-buffer pest.

If a "C-style" answer is given, IMO it would be better to use an
array which size can be justified and explained with accuracy, as
well as a limit value in the sprintf format string which makes sure
that it will not print more than what the buffer can store.
Jun 2 '07 #19

P: n/a
[long argument snipped]

Words fail me, I knew I would get jumped on for that code. I think my
bottom line is that there are more important things to worry about in
writing good code. My way has faults, so does any other way. The
differences are *really* small.

I think your bottom line is that you have a philosophical objection to
the 'arbitrariness'. It's not a problem for me.

BTW I said <stdio.hnot <cstdiobecause of the slight possibility that
the OP had an old C++ compiler.
Jun 3 '07 #20

This discussion thread is closed

Replies have been disabled for this discussion.