Results 1 to 8 of 8

Thread: LookUp table Implementation in Qt

  1. #1
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default LookUp table Implementation in Qt

    Hello friends i have a doubt.. i am working on a Qt based application where i show some temperature data. I have a look up table where i save a temperature value to the db for a decimal value.. Right now i have the decimal value data into a QString which i am comparing with the decimal value and appending the equivalent Temperature value into a QVector which i am saving into the database.
    Qt Code:
    1. if(str=="387")
    2. {
    3. temperaturedata.append("0");
    4. }else if(str=="414")
    5. {
    6. temperaturedata.append("10");
    7. }else if(str=="441")
    8. {
    9. temperaturedata.append("20");
    10. }else if(str=="469")
    11. {
    12. temperaturedata.append("30");
    13. }else if(str=="497")
    14. {
    15. temperaturedata.append("40");
    16. }else if(str=="525")
    17. {
    18. temperaturedata.append("50");
    19. }
    20. // And for all the other values also
    21. .
    22. .
    23. .
    To copy to clipboard, switch view to plain text mode 
    I wanted to know if there was a better way of handling a look up table, i have too many if else statements .. how could i handle this better..

    This is how the look up table is.
    Qt Code:
    1. Temperature Decimal
    2. -40 289
    3. -39 292
    4. -38 294
    5. -37 296
    6. -36 299
    7. -35 301
    8. -34 303
    9. -33 306
    10. -32 308
    11. -31 311
    12. -30 313
    13. -29 315
    14. -28 318
    15. -27 320
    16. -26 323
    17. -25 325
    18. -24 328
    19. -23 330
    20. -22 333
    21. -21 335
    22. -20 337
    23. -19 340
    24. -18 342
    25. -17 345
    26. -16 347
    27. -15 350
    28. -14 352
    29. -13 355
    30. -12 358
    31. -11 360
    32. -10 363
    33. -9 365
    34. -8 368
    35. -7 370
    36. -6 373
    37. -5 375
    38. -4 378
    39. -3 381
    40. -2 383
    41. -1 386
    42. 0 388
    43. 1 391
    44. 2 394
    45. 3 396
    46. 4 399
    47. 5 401
    48. 6 404
    49. 7 407
    50. 8 409
    51. 9 412
    52. 10 415
    53. 11 417
    54. 12 420
    55. 13 423
    56. 14 425
    57. 15 428
    58. 16 431
    59. 17 433
    60. 18 436
    61. 19 439
    62. 20 441
    63. 21 444
    64. 22 447
    65. 23 449
    66. 24 452
    67. 25 455
    68. 26 458
    69. 27 460
    70. 28 463
    71. 29 466
    72. 30 469
    73. 31 471
    74. 32 474
    75. 33 477
    76. 34 480
    77. 35 483
    78. 36 485
    79. 37 488
    80. 38 491
    81. 39 494
    82. 40 497
    83. 41 499
    84. 42 502
    85. 43 505
    86. 44 508
    87. 45 511
    88. 46 514
    89. 47 516
    90. 48 519
    91. 49 522
    92. 50 525
    To copy to clipboard, switch view to plain text mode 

    looking for some help.

    Thank you

  2. #2
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: LookUp table Implementation in Qt

    Instead of using all the else if statements did you consider using a QMap which seems perfect for these lookups.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: LookUp table Implementation in Qt

    Take a look at QHash, you can create a hash table mapping Decimal <-> temperature values, and instead of "if-else" statements simply call:
    Qt Code:
    1. // _hash contains your Decimal <-> temperature table
    2. if( _hash.contains(str) ){
    3. temperatureData.append( _hash.value(str) )
    4. }
    To copy to clipboard, switch view to plain text mode 

    ----
    Edit: looks like Stef was faster

  4. #4
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: LookUp table Implementation in Qt

    Hello thank you for the response. I did look at QMap but could't figure out how i should be doing it.. Din't find any example. Can you pls show a small example.

    i will try your method also stampede thank you so much for the example code.

    regards

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: LookUp table Implementation in Qt

    Have you looked at the examples in QMap documentation ?
    You just have to store values from your temperature table in such container ( initialize it once somewhere in the program ).

  6. #6
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: LookUp table Implementation in Qt

    It's not that hard really. First you populate the lookup table with all keys and values. Then if you want to use it you just ask for the key of a specific value.
    Qt Code:
    1. QMap<QString, QString> lookupTable;
    2.  
    3. // Populate lookup table
    4. lookupTable.insert("-40", "289");
    5. lookupTable.insert("-39", "292");
    6. lookupTable.insert("-38", "294");
    7. lookupTable.insert("-37", "296");
    8. lookupTable.insert("-36", "299");
    9. lookupTable.insert("-35", "301");
    10. // ... And so on
    11.  
    12. // Use it
    13. temperaturedata.append(lookupTable.value(str));
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Stef for this useful post:

    rex (9th February 2011)

  8. #7
    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: LookUp table Implementation in Qt

    Qt Code:
    1. QHash<int,int> lookUp;
    2. lookup[-40] = 289;
    3. lookup[-39] = 292;
    4. // etc.
    To copy to clipboard, switch view to plain text mode 

    or using a static C array:
    Qt Code:
    1. int valueFor(int v) {
    2. static int lookup[] = { 289, 292, 294, ... };
    3. return lookup[v+40];
    4. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    rex (9th February 2011)

  10. #8
    Join Date
    Dec 2010
    Posts
    41
    Thanks
    12

    Default Re: LookUp table Implementation in Qt

    thanks a lot i will do it now.. You made it look so easy.

Similar Threads

  1. Image resource lookup in OS X
    By stipa in forum Qt Quick
    Replies: 1
    Last Post: 1st December 2010, 08:11
  2. symbol lookup error
    By knishaq in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2010, 15:22
  3. QT-Embedded Symbol Lookup Error
    By augusbas in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 23rd June 2009, 05:01
  4. Dynamic lookup problem
    By jwintz in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2006, 14:19
  5. lookup table
    By georgie in forum General Programming
    Replies: 6
    Last Post: 12th May 2006, 09:57

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.