Results 1 to 14 of 14

Thread: Unit Converter Widget

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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:
    1. #define MU0 12.566370614e-7 /* magnetic constant */
    2. #define C0 299792458.0 /* speed of light in vacuum */
    3. #define ZF0 376.73031346958504364963 /* wave resistance in vacuum */
    4.  
    5. namespace Units
    6. {
    7. enum UnitType {
    8. Frequency=0,
    9. Length,
    10. Resistance,
    11. Angle,
    12. None = -1
    13. };
    14.  
    15. enum FrequencyUnits {
    16. GHz=0,Hz,KHz,MHz
    17. };
    18.  
    19. enum LengthUnits {
    20. mil=0,cm,mm,m,um,in,ft
    21. };
    22.  
    23. enum ResistanceUnits {
    24. Ohm=0,kOhm
    25. };
    26.  
    27. enum AngleUnits {
    28. Deg=0,Rad
    29. };
    30. const QStringList freqList(QStringList() << "GHz" << "Hz" << "kHz" << "MHz");
    31. const QStringList resList(QStringList() << "Ohm" << "kOhm");
    32. const QStringList lenList(QStringList() << "mil" << "cm" << "mm" << "m" << "um" << "in" << "ft");
    33. const QStringList angleList(QStringList() << "deg" << "rad");
    34.  
    35. QString toString(FrequencyUnits f);
    36. QString toString(LengthUnits l);
    37. QString toString(ResistanceUnits r);
    38. QString toString(AngleUnits a);
    39. QString toString(int un,UnitType t);
    40. int toInt(const QString& unit);
    41. double convert(double value, Units::UnitType ut, int fromUnit, int toUnit);
    42. };
    To copy to clipboard, switch view to plain text mode 

    unit.cpp
    Qt Code:
    1. namespace Units
    2. {
    3. // Unit conversion array for length.
    4. double lengthConversionTable[7][7] = {
    5. { 1.0, 2.54e-3, 2.54e-2, 2.54e-5, 25.4, 1.e-3, 1./12000},
    6. {1./2.54e-3, 1.0, 10.0, 1.e-2, 1.e4, 1./2.54, 1./30.48},
    7. {1./2.54e-2, 1./10., 1.0, 1.e-3, 1.e3, 1./25.4, 1./304.8},
    8. {1./2.54e-5, 1.e2, 1.e3, 1.0, 1.e6, 1./2.54e-2, 1./0.3048},
    9. {1./25.4, 1.e-4, 1.e-3, 1.e-6, 1.0, 1./2.54e4, 1./3.048e5},
    10. {1.e3, 2.54, 25.4, 2.54e-2, 2.54e4, 1.0, 1./12.},
    11. {1.2e4, 30.48, 304.8, 0.3048, 3.048e5, 12.0, 1.0}
    12. };
    13.  
    14. // Unit conversion array for frequencies.
    15. double frequencyConversionTable[4][4] = {
    16. { 1.0, 1.e9, 1.e6, 1.e3},
    17. { 1.e-9, 1.0, 1.e-3, 1.e-6},
    18. { 1.e-6, 1.e3, 1.0, 1.e-3},
    19. { 1.e-3, 1.e6, 1.e3, 1.0}
    20. };
    21.  
    22. // Unit conversion array for resistances.
    23. double resistanceConversionTable[2][2] = {
    24. {1.0, 1.e-3},
    25. {1.e3, 1.0}
    26. };
    27.  
    28. // Unit conversion array for angles.
    29. double angleConversionTable[2][2] = {
    30. {1.0, M_PI/180.0},
    31. {180.0/M_PI, 1.0}
    32. };
    33.  
    34. QString toString(FrequencyUnits f)
    35. {
    36. return Units::freqList[int(f)];
    37. }
    38. QString toString(LengthUnits l)
    39. {
    40. return Units::lenList[int(l)];
    41. }
    42. QString toString(ResistanceUnits r)
    43. {
    44. return Units::resList[int(r)];
    45. }
    46. QString toString(AngleUnits a)
    47. {
    48. return Units::angleList[int(a)];
    49. }
    50.  
    51. QString toString(int u, UnitType t)
    52. {
    53. switch(t) {
    54. case Frequency:
    55. return toString(FrequencyUnits(u));
    56. case Length:
    57. return toString(LengthUnits(u));
    58. case Resistance:
    59. return toString(ResistanceUnits(u));
    60. case Angle:
    61. return toString(AngleUnits(u));
    62. default:
    63. return QString();
    64. };
    65. }
    66.  
    67. double convert(double value, Units::UnitType ut, int fromUnit, int toUnit)
    68. {
    69. double cnv = value;
    70. switch(ut)
    71. {
    72. case Frequency:
    73. cnv *= frequencyConversionTable[int(fromUnit)][toUnit];
    74. break;
    75. case Length:
    76. cnv *= lengthConversionTable[int(fromUnit)][toUnit];
    77. break;
    78. case Resistance:
    79. cnv *= resistanceConversionTable[int(fromUnit)][toUnit];
    80. break;
    81. case Angle:
    82. cnv *= angleConversionTable[int(fromUnit)][toUnit];
    83. break;
    84. default:
    85. break;
    86. };
    87. return cnv;
    88. }
    89.  
    90. int toInt(const QString& unit)
    91. {
    92. if(unit == "NA")
    93. return None;
    94. for(int i=0; i<freqList.size(); ++i)
    95. {
    96. if(unit == freqList[i])
    97. return i;
    98. }
    99. for(int i=0; i<lenList.size(); ++i)
    100. {
    101. if(unit == lenList[i])
    102. return i;
    103. }
    104. for(int i=0; i<resList.size(); ++i)
    105. {
    106. if(unit == resList[i])
    107. return i;
    108. }
    109. for(int i=0; i<angleList.size(); ++i)
    110. {
    111. if(unit == angleList[i])
    112. return i;
    113. }
    114. return None;
    115. }
    116. };
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  4. #4
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Unit Converter Widget

    Quote Originally Posted by baray98 View Post
    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:
    1. enum TemperatureUnit {
    2. Celsius,
    3. Farenheit,
    4. Kelvin
    5. };
    6.  
    7. double convert(double val, TemperatureUnit from, TemperatureUnit to)
    8. {
    9. if(from == Celsius) {
    10. if(to == Kelvin)
    11. return val + 273.15;
    12. else if(to == Farenheit)
    13. return 1.8*val + 32;
    14. else
    15. return val;
    16. }
    17. else if(from == Farenheit) {
    18. if(to == Celsius)
    19. return (val - 32)*1.8;
    20. else if(to == Kelvin)
    21. return (val - 32)*1.8 + 273.15;
    22. else
    23. return val;
    24. }
    25. else {
    26. if(to == Celsius)
    27. return val - 273.15;
    28. else if(to == Farenheit)
    29. return (val-273.15)*1.8 + 32;
    30. else
    31. return val;
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unit Converter Widget

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

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unit Converter Widget

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

    baray98

  9. #9
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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>.

  10. #10
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Unit Converter Widget

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

    baray98
    Do you think is it ever possible ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unit Converter Widget

    Like a multiplication table from school

  13. #13

    Default 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. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Drawing a widget in QItemDelegate's paint method
    By darkadept in forum Qt Programming
    Replies: 17
    Last Post: 11th August 2009, 05:15
  2. Replies: 3
    Last Post: 17th October 2007, 12:52
  3. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  4. Controlling which widget on top layer?
    By JonathanForQT4 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2007, 14:27
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

Bookmarks

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.