Join Date
Aug 2007
Posts
275
Thanks
28
Thanked 2 Times in 2 Posts
Qt products
Platforms

## Unit Converter Widget

I was wondering if someone had made a unit converter widget , a widget that can convert the input from a unit (say meters) to a relative unit (say millimeters) i am more interested on the algorithm that they used to support lots of units

baray98

Join Date
Aug 2006
Location
Bangalore,India
Posts
419
Thanks
37
Thanked 53 Times in 40 Posts
Qt products
Platforms

## Re: Unit Converter Widget

I haven't done a general widget for unit conversion , so can't paste the code here.
But i had tried to generalize unit conversions. My skills were limited when i did this after Jacek had suggested me in this post.

I had come out with this some time back to help me convert between different units.
If some one has better idea, please do help so that i can improve my code too.

unit.h
Qt Code:
`#define MU0  12.566370614e-7          /* magnetic constant         */#define C0   299792458.0              /* speed of light in vacuum  */#define ZF0  376.73031346958504364963 /* wave resistance in vacuum */ namespace Units{  enum UnitType {    Frequency=0,    Length,    Resistance,    Angle,    None = -1  };   enum FrequencyUnits {    GHz=0,Hz,KHz,MHz  };   enum LengthUnits {    mil=0,cm,mm,m,um,in,ft  };   enum ResistanceUnits {    Ohm=0,kOhm  };   enum AngleUnits {    Deg=0,Rad  };  const QStringList freqList(QStringList() << "GHz" << "Hz" << "kHz" << "MHz");  const QStringList resList(QStringList() << "Ohm" << "kOhm");  const QStringList lenList(QStringList() << "mil" << "cm" << "mm" << "m" << "um" << "in" << "ft");  const QStringList angleList(QStringList() << "deg" << "rad");   QString toString(FrequencyUnits f);  QString toString(LengthUnits l);  QString toString(ResistanceUnits r);  QString toString(AngleUnits a);  QString toString(int un,UnitType t);  int toInt(const QString& unit);  double convert(double value, Units::UnitType ut, int fromUnit, int toUnit);};`
To copy to clipboard, switch view to plain text mode

unit.cpp
Qt Code:
`namespace Units{   // Unit conversion array for length.   double lengthConversionTable[7][7] = {      { 1.0,       2.54e-3,   2.54e-2,   2.54e-5,   25.4,     1.e-3,   1./12000},      {1./2.54e-3,    1.0,     10.0,      1.e-2,    1.e4,   1./2.54,   1./30.48},      {1./2.54e-2,  1./10.,     1.0,       1.e-3,   1.e3,   1./25.4,   1./304.8},      {1./2.54e-5,    1.e2,    1.e3,         1.0,   1.e6,  1./2.54e-2, 1./0.3048},      {1./25.4,      1.e-4,   1.e-3,       1.e-6,    1.0,  1./2.54e4,  1./3.048e5},      {1.e3,          2.54,    25.4,     2.54e-2, 2.54e4,        1.0,    1./12.},      {1.2e4,        30.48,   304.8,      0.3048, 3.048e5,      12.0,       1.0}   };    // Unit conversion array for frequencies.   double frequencyConversionTable[4][4] = {      { 1.0,     1.e9,     1.e6,      1.e3},      { 1.e-9,   1.0,      1.e-3,     1.e-6},      { 1.e-6,   1.e3,     1.0,       1.e-3},      { 1.e-3,   1.e6,     1.e3,      1.0}   };    // Unit conversion array for resistances.   double resistanceConversionTable[2][2] = {      {1.0,    1.e-3},      {1.e3,   1.0}   };    // Unit conversion array for angles.   double angleConversionTable[2][2] = {      {1.0,         M_PI/180.0},      {180.0/M_PI,         1.0}   };    QString toString(FrequencyUnits f)   {      return Units::freqList[int(f)];   }   QString toString(LengthUnits l)   {      return Units::lenList[int(l)];   }   QString toString(ResistanceUnits r)   {      return Units::resList[int(r)];   }   QString toString(AngleUnits a)   {      return Units::angleList[int(a)];   }    QString toString(int u, UnitType t)   {      switch(t) {         case Frequency:            return toString(FrequencyUnits(u));         case Length:            return toString(LengthUnits(u));         case Resistance:            return toString(ResistanceUnits(u));         case Angle:            return toString(AngleUnits(u));         default:            return QString();      };   }    double convert(double value, Units::UnitType ut, int fromUnit, int toUnit)   {      double cnv = value;      switch(ut)      {         case Frequency:            cnv *= frequencyConversionTable[int(fromUnit)][toUnit];            break;         case Length:            cnv *= lengthConversionTable[int(fromUnit)][toUnit];            break;         case Resistance:            cnv *= resistanceConversionTable[int(fromUnit)][toUnit];            break;         case Angle:            cnv *= angleConversionTable[int(fromUnit)][toUnit];            break;         default:            break;      };      return cnv;   }    int toInt(const QString& unit)   {      if(unit == "NA")         return None;      for(int i=0; i<freqList.size(); ++i)      {         if(unit == freqList[i])            return i;      }      for(int i=0; i<lenList.size(); ++i)      {         if(unit == lenList[i])            return i;      }      for(int i=0; i<resList.size(); ++i)      {         if(unit == resList[i])            return i;      }      for(int i=0; i<angleList.size(); ++i)      {         if(unit == angleList[i])            return i;      }      return None;   }};`
To copy to clipboard, switch view to plain text mode

3. ## Re: Unit Converter Widget

I think the best way would be to have a class (but not a widget) that reads definitions and formulas from an XML or ini file. Then it's just a matter of finding a proper formula, parsing and executing it. You can even keep scripts in the file and execute them using the script engine Qt provides.

Join Date
Aug 2007
Posts
275
Thanks
28
Thanked 2 Times in 2 Posts
Qt products
Platforms

## Re: Unit Converter Widget

Gopala I am lost in generating a temperatureConversion table

I need it in Celsius , Farenheit, Kelvin

hope you can help me out

baray98

Join Date
Aug 2006
Location
Bangalore,India
Posts
419
Thanks
37
Thanked 53 Times in 40 Posts
Qt products
Platforms

## Re: Unit Converter Widget

Originally Posted by baray98
Gopala I am lost in generating a temperatureConversion table

I need it in Celsius , Farenheit, Kelvin

hope you can help me out

baray98
I had done this some time before. May be this can be improved.
Qt Code:
`enum TemperatureUnit {  Celsius,  Farenheit,  Kelvin}; double convert(double val, TemperatureUnit from, TemperatureUnit to){  if(from == Celsius) {	if(to == Kelvin)	  return val + 273.15;	else if(to == Farenheit)	  return 1.8*val + 32;	else	  return val;  }  else if(from == Farenheit) {	if(to == Celsius)	  return (val - 32)*1.8;	else if(to == Kelvin)	  return (val - 32)*1.8 + 273.15;	else	  return val;  }  else {	if(to == Celsius)	  return val - 273.15;	else if(to == Farenheit)	  return (val-273.15)*1.8 + 32;	else	  return val;  }}`
To copy to clipboard, switch view to plain text mode

6. ## Re: Unit Converter Widget

Isn't this by any chance an academic/school problem?

7. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,714
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: Unit Converter Widget

Isn't this by any chance an academic/school problem?
I'd even say this is not a programming issue, this should be posted on a school math forum.
Once you have the formula, what is there to do? "hack" the "code"?
Come on...

Join Date
Aug 2007
Posts
275
Thanks
28
Thanked 2 Times in 2 Posts
Qt products
Platforms

## Re: Unit Converter Widget

I tought there was a way of converting temp using some kinda table but, thank you

baray98

9. ## Re: Unit Converter Widget

I made a Quantity class that did conversions for me, with Weight and Volume subclasses. It was based on Fowler's Quantity Analysis Pattern. You can get it at <http://www.usermode.org/code/quantity-0.1.tar.gz>.

Join Date
Aug 2006
Location
Bangalore,India
Posts
419
Thanks
37
Thanked 53 Times in 40 Posts
Qt products
Platforms

## Re: Unit Converter Widget

Originally Posted by baray98
I tought there was a way of converting temp using some kinda table but, thank you

baray98
Do you think is it ever possible ?

11. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,714
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: Unit Converter Widget

Do you think is it ever possible ?
For a defined range and resolution - sure!
Actually used by time critical applications, since its only a LUT lookup instead of a calculation.

12. ## Re: Unit Converter Widget

Like a multiplication table from school

13. Beginner
Join Date
Dec 2016
Posts
1

## Re: Unit Converter Widget

i want to know what is the main code for mm to cm to m widget .. gopala krishna can you help me with that.

14. ## Re: Unit Converter Widget

gopala krishna can you help me with that.
Really? You think digging up an 8 year old thread and asking one of the posters is going to help?

1 m = 100 cm = 1000 mm

It's a couple of lines of code - what would be the point of a widget to do that? Maybe you should be a little more clear about what it is you actually want to do.

Is this a homework project by any chance?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.