Results 1 to 7 of 7

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

  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.

  5. #5
    Join Date
    Apr 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

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

    Once I receive data (every 200ms) a signal is fired and update function is called...I can see the change in text but the change of color is what driving me crazy
    it is either (no effect with qpalatte) or too slow with stylesheets...

    I don't know if the no effect of qpalatte is due to Windows 7?

    I really appreciate your replies

    more details: a decoder class is firing the signal, and update function is called , update function resides in the GUI class

  6. #6
    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)

    Please refer the QPalette documentation, it has few warnings and notes about how not to use the QPalette, when QWidget's palette will be ignored.
    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.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

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

    There must be something else going on. This test program updates 100 widgets using style sheets in (typically) less than 10 milliseconds and it has to generate random data also. Is it slow on your machine?
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. double drand() {
    5. return (double)qrand()/(double)RAND_MAX;
    6. }
    7.  
    8. class Widget: public QWidget
    9. {
    10. Q_OBJECT
    11. QList<QLabel*> labels;
    12. QList<double> maxima;
    13.  
    14. public:
    15. explicit Widget(QWidget *p = 0): QWidget(p)
    16. {
    17. QGridLayout *layout = new QGridLayout(this);
    18. for (int r = 0; r < 10; ++r) {
    19. for (int c = 0; c < 10; ++c) {
    20. QLabel *label = new QLabel("0", this);
    21. layout->addWidget(label, r, c);
    22. labels << label;
    23. maxima << drand(); // a fake threshold value
    24. }
    25. }
    26.  
    27. QTimer *timer = new QTimer;
    28. connect(timer, SIGNAL(timeout()), SLOT(updateData()));
    29. timer->start(200);
    30. }
    31.  
    32. public slots:
    33.  
    34. void updateData() {
    35. static const QString red("color: rgb(255, 0, 0);");
    36. static const QString blue("color: rgb(0, 0, 255);");
    37.  
    38. QTime time;
    39. time.start();
    40. for (int i = 0; i < labels.size(); ++i) {
    41. QLabel * const label = labels.at(i);
    42. const double d = drand(); // fake data
    43. label->setText(QString::number(d, 'f', 3));
    44. if (d > maxima.at(i))
    45. label->setStyleSheet(red);
    46. else
    47. label->setStyleSheet(blue);
    48. }
    49. qDebug() << time.elapsed();
    50. }
    51. };
    52.  
    53.  
    54. int main(int argc, char **argv)
    55. {
    56. QApplication app(argc, argv);
    57.  
    58. Widget w;
    59. w.show();
    60.  
    61. return app.exec();;
    62. }
    63. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    There are a few obvious slow downs in your code. The style sheet fragments as constant and need only be created once. Your solution constructs a new QString every time and does an unnecessary conversion using fromUtf8(). You also make a function call to test the maximum, with the three parameters being copied in the process. Your data is a single precision number and you are comparing to a double maximum which causes a type conversion. If the maxima are constant declare them as such and let the compiler wring a few cycles out for you. The calls to setAutoFillBackground() are unneeded and may interfere with style sheets. If you really need it set it once on the widget and don't set it again.
    Last edited by ChrisW67; 16th April 2013 at 09:34.

Similar Threads

  1. Replies: 4
    Last Post: 2nd December 2010, 15:57
  2. How to install text color of an QLabel?
    By tumanovalex in forum Newbie
    Replies: 2
    Last Post: 24th September 2010, 12:41
  3. Replies: 3
    Last Post: 22nd January 2010, 17: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, 12:00
  5. QPalette won't set QLabel's Window & Foreground color
    By koklim in forum Qt Programming
    Replies: 6
    Last Post: 23rd January 2006, 11: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.