Results 1 to 7 of 7

Thread: How to change Qlabel text color (QPalette not working, Stylesheet too slow)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to change Qlabel text color (QPalette not working, Stylesheet too slow)

    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

  2. #2
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to change Qlabel text color (QPalette not working, Stylesheet too slow)

    use tr function to display text colors.
    lineEdit->setText(tr("<font color = red >"+your text+QString("\n")+QString(" </font size = 11 >")));

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to change Qlabel text color (QPalette not working, Stylesheet too slow)

    As such this is the only problem I see
    Qt Code:
    1. void Display::updateData()
    2. {
    3.  
    4. double max1 = 3.3;
    5. double max2 = 5.5;
    6. double max3 = 7.7;
    7.  
    8. //value 1
    9. //get the received value
    10. value1 = (receivedData->rec1);
    11. //check if the received value exceeds the max allowed value
    12. checkLimits(max1 ,value1, iuGUI->Qlabel1);
    13. //change the text of the label
    14. iuGUI->Qlabel1->setText(QString::number(value)); // should this be value1?
    15.  
    16. //value 2
    17. value2 = (receivedData->rec2);
    18. checkLimits(max2 ,value2, iuGUI->Qlabel2);
    19. iuGUI->Qlabel2->setText(QString::number(value));// should this be value2?
    20.  
    21. //value 3
    22. value3 = (receivedData->rec3);
    23. checkLimits(max3 ,value3, iuGUI->Qlabel3);
    24. iuGUI->Qlabel3->setText(QString::number(value));// should this be value3?
    25.  
    26. //etc....repeat the same for all the values...100!!
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    How often you call updateData(), and from where? (5Hz will be 200 ms), do you call once every 200 ms?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    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: How to change Qlabel text color (QPalette not working, Stylesheet too slow)

    How often you call updateData(), and from where? (5Hz will be 200 ms), do you call once every 200 ms?
    What Santosh is talking about is this: if you are calling updateData() from within a loop that is monitoring your signal, and your never get out of that loop to permit the Qt event loop to process pending events, then none of the changes to your labels will appear. Once every 200 ms is plenty of time to update the text and color of a label if you allow the event loop to run.

Similar Threads

  1. Replies: 4
    Last Post: 2nd December 2010, 14:57
  2. How to install text color of an QLabel?
    By tumanovalex in forum Newbie
    Replies: 2
    Last Post: 24th September 2010, 11:41
  3. Replies: 3
    Last Post: 22nd January 2010, 16:46
  4. Change color of a link in QLabel using Style Sheets?
    By codeslicer in forum Qt Programming
    Replies: 2
    Last Post: 15th April 2008, 11:00
  5. QPalette won't set QLabel's Window & Foreground color
    By koklim in forum Qt Programming
    Replies: 6
    Last Post: 23rd January 2006, 10:27

Tags for this Thread

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.