(gtk+) button with integer as its label

S

stinkinrich88

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!!!!!
 
J

John Harrison

stinkinrich88 said:
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, perhaps.

john
 
J

John Harrison

John said:
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
 
S

stinkinrich88

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!
 
J

John Harrison

stinkinrich88 said:
John said:
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
 
S

stinkinrich88

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

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...769376a109f/b2d605dcc086004b#b2d605dcc086004b
 
J

Juha Nieminen

John said:
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?
 
R

Robert Bauck Hamar

stinkinrich88 said:
John said:
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>
 
J

John Harrison

Juha said:
John said:
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
 
S

stinkinrich88

stinkinrich88 said:
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-...>

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*?
 
J

Juha Nieminen

stinkinrich88 said:
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*.
 
S

stinkinrich88

stinkinrich88 said:
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
 
S

stinkinrich88

stinkinrich88 said:
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-...>

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

John Harrison

Juha said:
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
 
J

Juha Nieminen

John said:
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.
 
J

John Harrison

Juha said:
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
 
B

BobR

John Harrison said:
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::eek:stringstreaam 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;
 
J

Juha Nieminen

BobR said:
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.
 
J

John Harrison

[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.h> not <cstdio> because of the slight possibility that
the OP had an old C++ compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top