OT? My First Working Modification of C++ Example Code, under QT. :-D

Feb 03, 2006 4 Replies

OK, I don't know if this is Off-Topic for the group(s), because "QT" isn't "Pure C++", and Slackware is a distro, but those guys are sharp. :-) And I've crossposted to sci.electroncs.design because I can. ;-P



Anyways, I want to crow about what I've done. I've been dabbling, kind of poking away at the QT tutorials, but when I get to the "Now, write some C++ to make it all go", I kinda hit a railroad crossing. ;-) So, I downloaded "Thinking in C++", and actually printed it on dead tree paper, and took it to bed with me to read myself to sleep. ;-) (it's about 4" thick, overall, printed single-sided on 20# letter-size. ;-) ) I really like the way that they mention, "Answers to the exercises are available for a nominal fee..."



So, anyways, I'm sitting here, kind of bored because there's no work and it really only takes a couple of hours to catch up on a day's worth of internet, and something bit me behind the knees, and I hauled out the ol' QT tutorial again, and popped up TICPPV1.pdf on another window, and in about an hour I'd produced:

formatting link
It doesnt' have any viruses, unless Gnu g++ put them there: $ file metric metric: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), not stripped (I didn't wrap this, but it's not that important).



But it's different from what came in the QT tutorial:



------------------------------------ void ConversionForm::convert() { enum MetricUnits { Kilometers, Meters, Centimeters, Millimeters }; enum OldUnits { Miles, Yards, Feet, Inches };



// Retrieve the input double input = numberLineEdit->text().toDouble(); double scaledInput = input;



// internally convert the input to millimeters switch ( fromComboBox->currentItem() ) { case Kilometers: scaledInput *= 1000000; break; case Meters: scaledInput *= 1000; break; case Centimeters: scaledInput *= 10; break; }



//convert to inches double result = scaledInput * 0.0393701;



switch ( toComboBox->currentItem() ) { case Miles: result /= 63360; break; case Yards: result /= 36; break; case Feet: result /= 12; break; }



// set the result int decimals = decimalsSpinBox->value(); resultLineEdit->setText( QString::number( result, 'f', decimals ) ); numberLineEdit->setText( QString::number( input, 'f', decimals ) ); }



----------------------------------



Which, quite frankly, based on what I've read in TICPPV1 and from lurking the NG, and a little diddling around on my own, looks like a really crappy excuse for the power of C++.



After poking around a bit with g++ and QT, I came up with this:



-------------------------------------- $ cat metric_conversion.ui.h /****************************************************************************



** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/ #include // Added by Rich: #include #include using namespace std;

void ConversionForm::init() { // Added by Rich: cout setText( "10" ); convert(); numberLineEdit->selectAll(); }



// Added by Rich: void ConversionForm::destroy() { cout currentItem() ) { case km: scaledInput *= 1000000; FromUnits = "km"; // this, and those below, make it break; // easy to report, below case m: scaledInput *= 1000; FromUnits = "m"; break; case cm: scaledInput *= 10; FromUnits = "cm"; break; }



//convert to inches double result = scaledInput * 0.0393701;



// like the above, for reporting string ToUnits = "In";



switch ( toComboBox->currentItem() ) { case Mi: result /= 63360; ToUnits = "Mi"; break; case Yd: result /= 36; ToUnits = "Yd"; break; case Ft: result /= 12; ToUnits = "Ft"; break; }



// set the result int decimals = decimalsSpinBox->value(); resultLineEdit->setText( QString::number( result, 'f', decimals ) ); numberLineEdit->setText( QString::number( input, 'f', decimals ) );



// I've added this part just to see console I/O simultaneously with pushing // buttons and schtuff. What a geek-gasm! cout text().toDouble()


Off toppic? no way! I was just looking for that... thanks :P

I see 4 errors. I'll fix 3 of them for you. See my inserted comments.

// the name was wrong

// so was this one

// You can have portability problems with this

The forth error was using C++ at all when real men program in ASM.

-- kensmith@rahul.net forging knowledge

Ha, ha. ;-)

Yeah - I hate this whole way of doing this, but the QT people wrote it. I've been mulling over a better way to do it, but I need more C++ learnin'. :-) I think if I do it properly I could add lots of different units and separate out "set conversion factor" from "convert", but this was basically just an exercise anyway - and the first one in the book, at that!

Forth error? It's not even Forth at all! ;-P

Cheers! Rich

In article , Rich Grise wrote: [...]

Better yet make a calculator program that lets you enter the inits and does the needed converting and grouping.

Like this:

123.45mW x 15Days = ####.####J

It could be a handly little desk top program.

-- kensmith@rahul.net forging knowledge

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required