Hi all,

I am working on a QT GUI application, I receive data from a microcontroller through serial at 5Hz and I am updating the GUI accordingaly

I use QLabel to display received values.


everything works fine, except that I want to change the text color of the QLabel when a value exceeds a certain limit, I have one hundered value to updated and using
stylesheet makes the GUI VERY SLOOOW, and if I use QPalette then there is no effect at all (the color is not changed)


here is my two version of the code



Qt Code:
  1. //using Stylesheets:
  2.  
  3. void GUI::checkLimits(double max, float value, QLabel * label){
  4.  
  5. if ((value > max)
  6. {
  7. label->setStyleSheet(QString::fromUtf8("color: rgb(255, 0, 0);\n"
  8. ""));
  9.  
  10. }else{
  11. label->setStyleSheet(QString::fromUtf8("color: rgb(0, 255, 255);\n"
  12. ""));
  13. }
  14. }
  15.  
  16.  
  17. //using QPalette:
  18.  
  19. void GUI::checkLimits(double max, float value, QLabel * label){
  20.  
  21. QPalette pal = label->palette ();
  22. if ((value > max)
  23. {
  24. pal->setColor(QPalette::WindowText, Qt::red);
  25.  
  26. }else{
  27. pal->setColor(QPalette::WindowText, Qt::white);
  28. }
  29.  
  30. label->setPalette (pal);
  31. label->setAutoFillBackground(true);
  32.  
  33. }
  34.  
  35.  
  36. //then call the function for each Qlabel:
  37.  
  38. //update the measurements as read from the PMU
  39. void Display::updateData()
  40. {
  41.  
  42. double max1 = 3.3;
  43. double max2 = 5.5;
  44. double max3 = 7.7;
  45.  
  46. //value 1
  47. //get the received value
  48. value1 = (receivedData->rec1);
  49. //check if the received value exceeds the max allowed value
  50. checkLimits(max1 ,value1, iuGUI->Qlabel1);
  51. //change the text of the label
  52. iuGUI->Qlabel1->setText(QString::number(value));
  53.  
  54. //value 2
  55. value2 = (receivedData->rec2);
  56. checkLimits(max2 ,value2, iuGUI->Qlabel2);
  57. iuGUI->Qlabel2->setText(QString::number(value));
  58.  
  59. //value 3
  60. value3 = (receivedData->rec3);
  61. checkLimits(max3 ,value3, iuGUI->Qlabel3);
  62. iuGUI->Qlabel3->setText(QString::number(value));
  63.  
  64. //etc....repeat the same for all the values...100!!
  65.  
  66. }
To copy to clipboard, switch view to plain text mode 

I am using QT 4.7 and running on Windows 7