LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 03-02-2010, 09:27 AM   #1
magoot
Member
 
Registered: Feb 2005
Location: Finland
Distribution: Debian testing
Posts: 38

Rep: Reputation: 15
Newbie needs help on C++ and GTKmm


Hello!
I decided to try and learn some C++ programming with GTKmm the other day, i have programmed a little C++ a couple of years back but nothing fancy.
I have been able to draw some inputs and buttons on the window but that's about as far as i can get at this point.
As you can see i'm trying to make a basic calculator that just adds two numbers together and displays the result.

The question here is how do i access the values from numberone and numbertwo entries and put the sum of these in the label answerlabel. I tried passing some arguments to the function calculate() but that didn't work.
Any tips on good ways to do this would be appreciated.

Here is the code so far:

main.cpp
Code:
/***************************************************************************
* file: main.cpp
*
*/
#include <gtkmm.h>
#include <iostream>


void calculate()
{
	std::cout << "Calculating..." << std::endl;
	/*
	* Calculate numone + numtwo here and
	* put the answer in "answerlabel"
	*/
}



int main(int argc, char *argv[])
{
	Gtk::Main kit(argc, argv);
	Gtk::Window mainwin;
	Gtk::Label pluslabel("+");
	Gtk::Label answerlabel("=");
	Gtk::VBox vbox;
	Gtk::HBox hbox;
	Gtk::Entry numone;
	Gtk::Entry numtwo;
	Gtk::Button calcbutton("Calculate");
	
	mainwin.set_title("Calculator");
	mainwin.set_border_width( 10 );
	mainwin.set_default_size( 300, 100 );
	mainwin.add(vbox);
	
	hbox.pack_start(numone, false, false, 0);
	hbox.pack_start(pluslabel, false, false, 5);
	hbox.pack_start(numtwo, false, false, 0);
	vbox.pack_start(hbox, false, false, 0);
	vbox.pack_start(answerlabel, false, false, 5);
	vbox.pack_start(calcbutton, false, false, 5);
	
	pluslabel.show();
	answerlabel.show();
	hbox.show();
	vbox.show();
	calcbutton.show();
	numone.show();
	numtwo.show();
	
	calcbutton.signal_clicked().connect( sigc::ptr_fun( calculate ) );
	
	Gtk::Main::run( mainwin );
	
	return 0;
}
Makefile
Code:
# Makefile

CFLAGS = `pkg-config gtkmm-2.4 --cflags --libs`
OPTIONS = -Wall

all:
	g++ $(OPTIONS) main.cpp -o main $(CFLAGS)
 
Old 03-03-2010, 02:27 AM   #2
leslieviljoen
LQ Newbie
 
Registered: Sep 2008
Posts: 10

Rep: Reputation: 1
You need to access the 'text' properties of numone and numtwo, and you will need to convert those text properties to floats (with atof maybe).

And since you want to access them from another function, you will have to move the declarations:

Gtk::Entry numone;
Gtk::Entry numtwo;

..outside function main.

For Gtk reference:
http://library.gnome.org/devel/gtk/u...GtkEntry--text

For an example of some similar code:
http://www.kplug.org/glade_tutorial/...l/gtemp_4.html
 
Old 03-03-2010, 05:39 PM   #3
m95vebj
LQ Newbie
 
Registered: Jul 2008
Location: Sweden
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
But you wanted C++? so maybe you could do something like...


#include <sstream>

....

std::stringstream ss;
float some_float, another_float;
string some_string;
....

// Get the string and Cast to float...
ss << numone.get_text();
ss >> some_float;
ss << numtwo.get_text();
ss >> another_float;
....

some_float = do something with some_float & another_float;

// Cast back to string...
ss << some_float;
ss >> some_string;

// and present the result in numthree

numthree.set_text(some_string)
 
Old 03-05-2010, 01:56 PM   #4
m95vebj
LQ Newbie
 
Registered: Jul 2008
Location: Sweden
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
Smile

Hello Magoot,

have you found out how to proceed yet? If not please find here one possible solution in C++ with classes and everything. I think it is best to look at the examples bundled with Gtkmm to learn how to make a structured program. I split up the code on three files and updated the makefile accordingly. Have fun!

//simple_calculator.h

#ifndef GTKMM_SIMPLE_CALCULATOR_H
#define GTKMM_SIMPLE_CALCULATOR_H

#include <gtkmm.h>

class Simple_Calculator : public Gtk::Window
{
public:
Simple_Calculator();
virtual ~Simple_Calculator();

protected:
//Signal handlers:
void calculate();


//Child widgets:
Gtk::VBox vbox;
Gtk::HBox hbox;

Gtk::Entry numone, numtwo;
Gtk::Button calcbutton;
Gtk::Label pluslabel, answerlabel;
};

#endif //GTKMM_SIMPLE_CALCULATOR_H



// simple_calculator.cc
#include "simple_calculator.h"
#include <iostream>
#include <sstream>
#include <string.h>
#include <string>


Simple_Calculator::Simple_Calculator()
: calcbutton("Calculate"),
pluslabel("+"),
answerlabel("=")
{
set_size_request(360, 100);
set_title("My simple Calculator");
set_border_width(10);

add(vbox);

hbox.pack_start(numone, false, false, 0);
hbox.pack_start(pluslabel, false, false, 5);
hbox.pack_start(numtwo, false, false, 0);
vbox.pack_start(hbox, false, false, 0);
vbox.pack_start(answerlabel, false, false, 5);
vbox.pack_start(calcbutton, false, false, 5);

calcbutton.signal_clicked().connect( sigc::mem_fun(*this,
&Simple_Calculator::calculate) );
calcbutton.set_flags(Gtk::CAN_DEFAULT);
calcbutton.grab_default();

show_all_children();
}

Simple_Calculator::~Simple_Calculator()
{
}

void Simple_Calculator::calculate()
{
std::stringstream ss, ss2, ss3;
float first, second;
std::string result;

ss << numone.get_text();
ss >> first;
ss2 << numtwo.get_text();
ss2 >> second;
first = first + second;
ss3 << first;
ss3 >> result;
answerlabel.set_text("=" + result);
}



//my_calculator.cc

#include <gtkmm/main.h>
#include "simple_calculator.h"

int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);

Simple_Calculator calculator;
//Shows the window and returns when it is closed.
Gtk::Main::run(calculator);

return 0;
}



# Makefile

CFLAGS = `pkg-config gtkmm-2.4 --cflags --libs`
OPTIONS = -Wall

all:
g++ $(OPTIONS) -o my_calculator my_calculator.cc simple_calculator.cc $(CFLAGS)
 
Old 03-06-2010, 07:11 PM   #5
magoot
Member
 
Registered: Feb 2005
Location: Finland
Distribution: Debian testing
Posts: 38

Original Poster
Rep: Reputation: 15
Hi!
Sorry for a late reply, haven't had time to look at this latley.
Thanks a lot for your replys, it helps a lot starting to understand better how to structure programs now.

Your example was great m95vebj, however i had a question:
This row is a bit confusing for me:
Simple_Calculator::Simple_Calculator() : calcbutton("Calculate"), pluslabel("+"), answerlabel("=")
what exactly needs to be added to this row? As far as i can see it contains all widgets except numone and numtwo?
If i wanted to add more labels and stuff, do these go on this row as well?

Thanks.
// magoot
 
Old 03-07-2010, 02:42 AM   #6
m95vebj
LQ Newbie
 
Registered: Jul 2008
Location: Sweden
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
Quote:
Originally Posted by magoot View Post
Hi!
Sorry for a late reply, haven't had time to look at this latley.
Thanks a lot for your replys, it helps a lot starting to understand better how to structure programs now.

Your example was great m95vebj, however i had a question:
This row is a bit confusing for me:
Simple_Calculator::Simple_Calculator() : calcbutton("Calculate"), pluslabel("+"), answerlabel("=")
what exactly needs to be added to this row? As far as i can see it contains all widgets except numone and numtwo?
If i wanted to add more labels and stuff, do these go on this row as well?

Thanks.
// magoot


Hi again,

Those lines initiates the widgets and puts the initial text on them. I suppose you could have used e.g. answerlabel.add_text("="); later on in stead. You do however need to declare new widgets in the class declaration to be useful.
 
Old 10-05-2010, 05:53 PM   #7
carlos22
LQ Newbie
 
Registered: Oct 2004
Location: Algeria
Distribution: Debial lenny 32 bits+ Fedora 13-x86_64 /P4 3.2 Ghz 2 GB(Ram)
Posts: 21

Rep: Reputation: 0
hi,

Thanks a lot for these.
It helped me do this function:

class topwindow : public Gtk::Window
{
public:
topwindow();
...
...


virtual void ADD();
Gtk::Entry result_entry;

...
...

protected:
...
...

Gtk::Entry input_1, input_2;

...
...

};

void topwindow::ADD()
{
std::stringstream ss, ss2, ss3;
double first, second, total;
std::string resulting;

ss << input_1.get_text();
ss >> first;
ss2 << input_2.get_text();
ss2 >> second;
total = first + second;
ss3 << total;
ss3 >> resulting;
result_entry.set_text(resulting);

}

Hope this helps.
 
  


Reply

Tags
c++, gtkmm


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gtkmm strycnine Slackware 6 12-12-2007 03:46 PM
GTKmm and SigC stonehurstX11 Programming 4 05-04-2005 02:17 PM
gtkmm help paulr1984 Linux - Software 1 03-28-2005 02:40 AM
GTKmm Help gtkmike Programming 10 09-22-2004 08:32 AM
gtkmm HOW? Barq Programming 1 08-29-2004 07:43 AM


All times are GMT -5. The time now is 02:38 PM.

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