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

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

P: n/a
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:
http://www.abiengr.com/~sysop/Share/metric
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:
---------------<quote>---------------------
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 ) );
}
--------------<end quote>--------------------

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:
------------<quote>--------------------------
$ 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 <qvalidator.h>
// Added by Rich:
#include <iostream>
#include <string>
using namespace std;

void ConversionForm::init()
{
// Added by Rich:
cout << "Conversion Form Initializing" << endl;
numberLineEdit->setValidator( new QDoubleValidator( numberLineEdit ) );
numberLineEdit->setText( "10" );
convert();
numberLineEdit->selectAll();
}

// Added by Rich:
void ConversionForm::destroy()
{
cout << "Conversion Form Being Removed" << endl;
}

void ConversionForm::convert()
{
// Changed from "Kilometers", etc, to "km" etc
enum MetricUnits {
km,
m,
cm,
mm
};
enum OldUnits {
Mi,
Yd,
Ft,
In
};

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

// internally convert the input to millimeters

// added this line - the string is my idea
string FromUnits = "mm";

switch ( fromComboBox->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 << numberLineEdit->text().toDouble() << " " << FromUnits
<< " = " << resultLineEdit->text().toDouble() << " " << ToUnits <<"." << endl;
}

--------------------</quote>-----------------

So, anyways, I wanted to show off my accomplishment: the executable is at
the link above, and here:
http://www.abiengr.com/~sysop/Share/metric - it's an ELF 32-bit
executable, but I don't know if you need QT installed on your machine. I'd
think anybody running any reasonable distro with X up should be able to
D/L that and just run it. In fact, I'd be very, very interested to hear
of anyone's experiences running it - it doesn't have any malware, unless
g++ or qmake or make put it there. ;-)

Oh, it was also kewl to look at some of the XML and comprehend it. ;-)

OK, on proofreading - do my new strings get deleted when I fall out of
scope, or should I explicitly delete them in the destructor? If I got what
I think I read, they automatically get destructed, since they're
created locally, the compiler automatically destructs them when I
leave the scope normally, is this accurate?

Still proofreading - I'd like to granularize that scale factor schtuff -
like come up with one scale factor, so that convert() only had to go
output = input * scalefactor; but I need some more learning about
objects and stuff - I'm somewhat disappointed, or don't know where to
look, that the dropdown box doesn't seem to have a property where I can
retrieve its item's text. Oh, well, At least I got it going the way I
want it to, except for the fact that when I hit "enter" on the text
input box, it clears the box. I don't know how to not make it do that.

Wanna see the XML of the form? Well, it's only a text NG, and this
is the last part of the post, so you can skip it if you want:
----------------<quote>-------------------------
$ cat metric_conversion.ui
<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
<class>ConversionForm</class>
<widget class="QDialog">
<property name="name">
<cstring>ConversionForm</cstring>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>402</width>
<height>127</height>
</rect>
</property>
<property name="caption">
<string>Metric -&gt; Inches Conversion</string>
</property>
<vbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QLayoutWidget">
<property name="name">
<cstring>layout8</cstring>
</property>
<hbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QLineEdit">
<property name="name">
<cstring>numberLineEdit</cstring>
</property>
<property name="alignment">
<set>AlignAuto</set>
</property>
<property name="toolTip" stdset="0">
<string>Metric Value</string>
</property>
<property name="whatsThis" stdset="0">
<string>Input your measurement here, and select units km, m, cm, or mm on the drop-down box to the right.</string>
</property>
</widget>
<widget class="QComboBox">
<item>
<property name="text">
<string>km</string>
</property>
</item>
<item>
<property name="text">
<string>m</string>
</property>
</item>
<item>
<property name="text">
<string>cm</string>
</property>
</item>
<item>
<property name="text">
<string>mm</string>
</property>
</item>
<property name="name">
<cstring>fromComboBox</cstring>
</property>
</widget>
<widget class="QLabel">
<property name="name">
<cstring>textLabel2</cstring>
</property>
<property name="text">
<string>=</string>
</property>
<property name="buddy" stdset="0">
<cstring>fromComboBox</cstring>
</property>
</widget>
<widget class="QLineEdit">
<property name="name">
<cstring>resultLineEdit</cstring>
</property>
<property name="sizePolicy">
<sizepolicy>
<hsizetype>7</hsizetype>
<vsizetype>0</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="paletteBackgroundColor">
<color>
<red>255</red>
<green>255</green>
<blue>127</blue>
</color>
</property>
<property name="alignment">
<set>AlignRight</set>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QComboBox">
<item>
<property name="text">
<string>Mi</string>
</property>
</item>
<item>
<property name="text">
<string>Yd</string>
</property>
</item>
<item>
<property name="text">
<string>Ft</string>
</property>
</item>
<item>
<property name="text">
<string>In</string>
</property>
</item>
<property name="name">
<cstring>toComboBox</cstring>
</property>
</widget>
</hbox>
</widget>
<widget class="QLayoutWidget">
<property name="name">
<cstring>layout5</cstring>
</property>
<hbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QLabel">
<property name="name">
<cstring>textLabel5</cstring>
</property>
<property name="text">
<string>&amp;Precision</string>
</property>
<property name="buddy" stdset="0">
<cstring>decimalsSpinBox</cstring>
</property>
</widget>
<spacer>
<property name="name">
<cstring>spacer1</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>170</width>
<height>20</height>
</size>
</property>
</spacer>
<widget class="QSpinBox">
<property name="name">
<cstring>decimalsSpinBox</cstring>
</property>
<property name="maxValue">
<number>6</number>
</property>
<property name="value">
<number>3</number>
</property>
</widget>
<widget class="QLabel">
<property name="name">
<cstring>textLabel1</cstring>
</property>
<property name="text">
<string>Decimal Places</string>
</property>
</widget>
</hbox>
</widget>
<spacer>
<property name="name">
<cstring>spacer3</cstring>
</property>
<property name="orientation">
<enum>Vertical</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>20</width>
<height>16</height>
</size>
</property>
</spacer>
<widget class="QLayoutWidget">
<property name="name">
<cstring>layout3</cstring>
</property>
<hbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QPushButton">
<property name="name">
<cstring>clearPushButton</cstring>
</property>
<property name="text">
<string>&amp;Clear</string>
</property>
<property name="accel">
<string>Alt+C</string>
</property>
</widget>
<widget class="QPushButton">
<property name="name">
<cstring>calcluatePushButton</cstring>
</property>
<property name="text">
<string>Calculate</string>
</property>
<property name="accel">
<string></string>
</property>
</widget>
<spacer>
<property name="name">
<cstring>spacer2</cstring>
</property>
<property name="orientation">
<enum>Horizontal</enum>
</property>
<property name="sizeType">
<enum>Expanding</enum>
</property>
<property name="sizeHint">
<size>
<width>41</width>
<height>20</height>
</size>
</property>
</spacer>
<widget class="QPushButton">
<property name="name">
<cstring>quitPushButton</cstring>
</property>
<property name="text">
<string>&amp;Quit</string>
</property>
<property name="accel">
<string>Alt+Q</string>
</property>
</widget>
</hbox>
</widget>
</vbox>
</widget>
<connections>
<connection>
<sender>clearPushButton</sender>
<signal>clicked()</signal>
<receiver>numberLineEdit</receiver>
<slot>clear()</slot>
</connection>
<connection>
<sender>clearPushButton</sender>
<signal>clicked()</signal>
<receiver>resultLineEdit</receiver>
<slot>clear()</slot>
</connection>
<connection>
<sender>clearPushButton</sender>
<signal>clicked()</signal>
<receiver>numberLineEdit</receiver>
<slot>setFocus()</slot>
</connection>
<connection>
<sender>quitPushButton</sender>
<signal>clicked()</signal>
<receiver>ConversionForm</receiver>
<slot>close()</slot>
</connection>
<connection>
<sender>calcluatePushButton</sender>
<signal>clicked()</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
<connection>
<sender>decimalsSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
<connection>
<sender>fromComboBox</sender>
<signal>textChanged(const QString&amp;)</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
<connection>
<sender>toComboBox</sender>
<signal>textChanged(const QString&amp;)</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
<connection>
<sender>numberLineEdit</sender>
<signal>returnPressed()</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
<connection>
<sender>numberLineEdit</sender>
<signal>lostFocus()</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
<connection>
<sender>fromComboBox</sender>
<signal>activated(const QString&amp;)</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
<connection>
<sender>toComboBox</sender>
<signal>activated(const QString&amp;)</signal>
<receiver>ConversionForm</receiver>
<slot>convert()</slot>
</connection>
</connections>
<tabstops>
<tabstop>numberLineEdit</tabstop>
<tabstop>fromComboBox</tabstop>
<tabstop>resultLineEdit</tabstop>
<tabstop>toComboBox</tabstop>
<tabstop>decimalsSpinBox</tabstop>
<tabstop>clearPushButton</tabstop>
<tabstop>calcluatePushButton</tabstop>
<tabstop>quitPushButton</tabstop>
</tabstops>
<includes>
<include location="local" impldecl="in implementation">metric_conversion.ui.h</include>
</includes>
<slots>
<slot>convert()</slot>
</slots>
<functions>
<function access="private" specifier="non virtual">init()</function>
<function access="private" specifier="non virtual">destroy()</function>
</functions>
<pixmapinproject/>
<layoutdefaults spacing="6" margin="11"/>
</UI>
--------------</quote>--------------------------

Thanks!
Rich

Feb 3 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Rich Grise wrote:
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


Off toppic? no way! I was just looking for that... thanks :P
Feb 3 '06 #2

P: n/a
In article <pa****************************@example.net>,
Rich Grise <ri*******@example.net> wrote:

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

---------------<quote>---------------------
void ConversionForm::convert()
{ // the name was wrong enum SillyPeopleUnits {
Kilometers,
Meters,
Centimeters,
Millimeters
}; // so was this one enum BetterUnits {
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: // You can have portability problems with this //scaledInput *= 1000000;
scaledInput *= 1000000.0;
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 ) );
}
--------------<end quote>--------------------


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

--
--
ke******@rahul.net forging knowledge

Feb 3 '06 #3

P: n/a
Tom
On Fri, 03 Feb 2006 00:47:31 GMT, Rich Grise <ri*******@example.net>
wrote:
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..."


Hey Rich --

I'm a newbie here and I say excellent post!!

A quote from Cline/Lomow:

"Composition (sometimes called aggregation) is the process of putting
an object (a part) inside another object (the composite)."

Seems to me in here a LOT of the conversation (and justifiably so) is
about the aggregation of STL (Standard Template Library) into a user
written program. Learning the capabilities and all the reference
pointer and template stuff is darn tricky. But the end result is a lot
of power without having to re-invent the wheel. Or having to be as
competent and skilled as the author of the template.

In my opinion, C++ folks are considered by the non-C++ folks as
guru's. Regardless of skill level. Other peeps are just intimidated by
it all. So when a poor, unsuspecting soul posts something that a
narrow minded aggregationist of the STL thinks is "Off Topic" ... they
can get a flame thrown at them. HA !!

C++ is about finding solutions and living inside and outside the C++
box. Don't let the self appointed OT cops flavor your wheaties with
their venom.

I propose a grading scheme:

+5 points for posting what appears to be an honest and reasonable
topic or question. Inside and outside the box.

+3 points for providing the best answer to a posted question.

+10 points for providing a link and/or justifying your position in
enough detail that someone can learn from it.

+1 points for a failed, but honest attempt trying to help someone.

-10 pts for being self appointed cop or replying in a way that does
not contribute to solving the problem. Throw in a few of your own
favorite adjectives to describe this type of person .. :))

I really don't understand the ego that makes someone go into the OT
attack mode. Heck, just ignore the author or read on. No one says you
have to read all the traffic in here !! Scan fast and get a life.

Oh, and another grade >>

-20 pts for defending another OT cop when they are challenged for
being such jerks. But only challenged when the pattern is well
established and undeniable. HAHAHA.

Sort by name and grade a few. The patterns become obvious.

A brainiac just might be negative on the pts tally.

----------

I look forward to your next post.

Rebel is as rebel does!

-- Tom
Feb 3 '06 #4

P: n/a
Tom wrote:
I'm a newbie here and I say excellent post!!

I propose a grading scheme:
How new are you? Do you understand the community well enough to be able
to grade everybody here?
+5 points for posting what appears to be an honest and reasonable
topic or question. Inside and outside the box.
The box is the C++ language. Outside the box is OT.
+3 points for providing the best answer to a posted question.
How do you quantify "best"? Fastest coding, fastest runtime,
explanation with words of fewest average syllables etc, etc.
+10 points for providing a link and/or justifying your position in
enough detail that someone can learn from it.
Why is a link worth 3.3 times more than, say a detailed analysis of the
problem domain and a codified solution?
+1 points for a failed, but honest attempt trying to help someone.
Such as telling somebody to find a newsgroup more appropriate to their
question, so that people more knowledgeable on the subject material can
reply and be peer reviewed, resulting in the better answer. Increasing
the signal/noise ratio here so that the archive can be kept clutter
free, and remains a useful, search able database of solutions to C++
Language problems, helping everybody search content. Reducing noise
here, so that time can be spent solving peoples Language problems (The
topic of the newsgroup) without sifting through miles of stuff that
should be in a more relevant forum.
-10 pts for being self appointed cop or replying in a way that does
not contribute to solving the problem. Throw in a few of your own
favorite adjectives to describe this type of person .. :))
How much for introducing a scoring system that will not work, whilst
increasing the noise ratio of the newsgroup and making suggestions to
disrupt the happy community?
I really don't understand the ego that makes someone go into the OT
attack mode. Heck, just ignore the author or read on. No one says you
have to read all the traffic in here !! Scan fast and get a life.
As mentioned above. To keep this place worthwhile. If it devolves into
a general discussion group about anything that includes the letter C, it
will no longer be useful to anybody.
Oh, and another grade >>


....bored already.

I think you'll find people here couldn't give a flying pig about a
misguided rewards system.

In many situations, rewards systems have a negative impact on people
getting done what needs to be done, in favour of looking good. I don't
think that would apply here, at least not to those who give the most
back to the community.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 3 '06 #5

P: n/a
On Fri, 03 Feb 2006 02:12:30 +0000, Ken Smith wrote:
In article <pa****************************@example.net>, Rich Grise
<ri*******@example.net> wrote:

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

// the name was wrong
enum SillyPeopleUnits { // so was this one
enum BetterUnits {
Ha, ha. ;-)

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

// internally convert the input to millimeters switch (
fromComboBox->currentItem() ) { case Kilometers:

// You can have portability problems with this
//scaledInput *= 1000000;


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!
The forth error was using C++ at all when real men program in ASM.


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

Cheers!
Rich
Feb 3 '06 #6

P: n/a
Tom wrote:
I really don't understand the ego that makes someone go into the OT
attack mode. Heck, just ignore the author or read on. No one says you
have to read all the traffic in here !! Scan fast and get a life.


There are a couple of reasons why the "OT Cops" are verbal.

1. In a group dedicated to answering C++ specific questions, the
experts that answer questions are focused on C++. They are not
necessarily experts on platform-specific questions, or programming
domain specific questions. But there are groups dedicated to other
topics.

2. Keeping the first point in mind, imagine that you are searching for
an answer to a problem that you have. Would you rather trust a
solution in a group specific to your problem domain, or in a group that
is semi-topical?

An example would be, suppose I post a question to comp.lang.c++ about a
decent C++ algorthim to determine sentence boundries, in terms of
statistical NLP. I don't know if there are any NLP gurus in here, so
it wouldn't make any sense. If I got an answer -- even if correct --
anyone reading the message in the future would have to decide if they
could trust the information presented. This is easier when the
question is topical to the area where the question was asked.

3. Not everyone uses Google Groups. I used to use a news reader and
Off-topic posts [such as this one] polluted the "In-Box," which made it
more difficult to find the good posts . comp.lang.c++ is a
high-traffic news group, and realistically no one wants to sort through
cruft (expect for those who argue against the OT cops).

If you want a point based Q&A type of thing, go to Experts Exchange.

~KS

Feb 3 '06 #7

P: n/a
In article <pa**************************@example.net>,
Rich Grise <ri*******@example.net> wrote:
[...]
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!


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.
--
--
ke******@rahul.net forging knowledge

Feb 4 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.