Results 1 to 9 of 9

Thread: QLabel->setText() sometimes not working

  1. #1
    Join Date
    May 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QLabel->setText() sometimes not working

    I have created an application with a countdown timer and the number of seconds remaining is displayed in a QLabel widget inside the Graphics View Framework.

    In the code below time_label is a QLabel and time_bar is a QProgressBar. Both widgets are wrapped inside a QGraphicsProxyWidgets which are displayed on a QGraphicsScene. The method timerEvent is triggered every 1000ms. The onTestOver() method kills the timer once the time is up. I had left out the irrelevant code on purpose.

    Qt Code:
    1. void TestPlan::timerEvent(QTimerEvent* tick)
    2. {
    3. time_bar->setValue(time_bar->value()+1);
    4.  
    5. int max = time_bar->maximum();
    6. int val = time_bar->value();
    7.  
    8. int time_left = max - val;
    9. qDebug() << time_left;
    10.  
    11. if (time_left > 0)
    12. {
    13. time_label->setText(tr("%1 seconds remaining").arg(time_left));
    14. }
    15. else
    16. {
    17. time_label->setText(tr("Time is up!"));
    18. onTestOver();
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    In Qt Creator's "Application Output" I can see that timerEvent() is called every second so the timer works fine:

    Qt Code:
    1. 14
    2. 13
    3. 12
    4. 11
    5. 10
    6. 9
    7. 8
    8. 7
    9. 6
    10. 5
    11. 4
    12. 3
    13. 2
    14. 1
    15. 0
    To copy to clipboard, switch view to plain text mode 

    However the output on the application whether using Debug or Release build I see the count down from 14 to 12, 11 is skipped, then from 10 to 5, 4 is skipped again, and the from 3 to 0.

    I though maybe the QLabel updates slowly and I tried in increase the time per interval to 2000ms, but had the same result. The progressbar which also updates by this method, updates correctly every second. It is just the QLabel that sometimes seems to skip an update.

    Any ideas on how I can make the QLabel widget to update correctly?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel->setText() sometimes not working

    Try calling label->update and see if it works..

    and you may also post compilable code that has this problem...it wud be easy to verify for others

  3. #3
    Join Date
    May 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLabel->setText() sometimes not working

    Try calling label->update and see if it works..
    Thank you for the quick response, but label->update() did not work.

    you may also post compilable code that has this problem
    Unfortunately this code is part of a bigger project that contains thousands of lines of code.

  4. #4
    Join Date
    May 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLabel->setText() sometimes not working

    I reproduced the error noticed by the original project to a smaller compilable project:

    Qt Code:
    1. #include <QtGui>
    2.  
    3.  
    4. class TestPlan: public QObject
    5. {
    6. QProgressBar* time_bar;
    7. QLabel* time_label;
    8. int timer_id;
    9.  
    10. void timerEvent(QTimerEvent* tick)
    11. {
    12. time_bar->setValue(time_bar->value()+1);
    13.  
    14. int max = time_bar->maximum();
    15. int val = time_bar->value();
    16.  
    17. int time_left = max - val;
    18. qDebug() << time_left;
    19.  
    20. if (time_left > 0)
    21. {
    22. time_label->setText(tr("%1 seconds remaining").arg(time_left));
    23. }
    24. else
    25. {
    26. time_label->setText(tr("Time is up!"));
    27. onTestOver();
    28. }
    29. }
    30.  
    31. void onTestOver()
    32. {
    33. if (timer_id) killTimer(timer_id);
    34. }
    35.  
    36. public:
    37. TestPlan()
    38. {
    39. QWidget* widget = new QWidget();
    40. scene->addWidget(widget);
    41.  
    42. QHBoxLayout* layout = new QHBoxLayout();
    43. widget->setLayout(layout);
    44.  
    45. time_bar = new QProgressBar(widget);
    46. time_bar->setValue(0);
    47. time_bar->setMaximum(15);
    48. layout->addWidget(time_bar);
    49.  
    50. time_label = new QLabel(widget);
    51. layout->addWidget(time_label);
    52.  
    53. timer_id = startTimer(1000);
    54. }
    55. };
    56.  
    57. int main(int argc, char *argv[])
    58. {
    59. QApplication app(argc, argv);
    60.  
    61. QWidget* window = new QWidget();
    62.  
    63. QGridLayout* layout = new QGridLayout(window);
    64. window->setLayout(layout);
    65.  
    66. scene = new QGraphicsScene(window);
    67. scene->setSceneRect(0,0,window->width(),window->height());
    68.  
    69. layout->addWidget(new QGraphicsView(scene, window));
    70.  
    71. TestPlan test;
    72.  
    73. window->show();
    74.  
    75. app.exec();
    76.  
    77. delete window;
    78.  
    79. return 0;
    80. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: QLabel->setText() sometimes not working

    You are using the timer a bit incorrectly, that's for sure (you can't expect it to fire every exact 1000ms) but I don't know if that's a reason for what you are experiencing. First of all try getting Graphics View architecture out of the equation - reproduce the problem using a plain QLabel.
    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.


  6. #6
    Join Date
    May 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLabel->setText() sometimes not working

    You are using the timer a bit incorrectly
    Then how SHOULD I use the timer?

    I don't know if that's a reason for what you are experiencing
    The QProgressBar updates fine using the same timer and the same method!

    First of all try getting Graphics View architecture out of the equation - reproduce the problem using a plain QLabel.
    I know for a small project like this, it is silly to use the Graphics View architecture, but for the original project this is a requirement. I did however removed the Graphics View architecture as you suggested and then the QLabel updated correctly. However the original project still requires the Graphics View architecture for animations and stuff and I was under the impression that Qt 4.6.2 is a stable release.
    Last edited by cass; 15th May 2010 at 16:11. Reason: spelling corrections

  7. #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: QLabel->setText() sometimes not working

    Quote Originally Posted by cass View Post
    Then how SHOULD I use the timer?
    You should check the time elapsed instead of bumping the value one by one. To simplify the situation you could use QTimeLine.

    The QProgressBar updates fine using the same timer and the same method!
    Like I said, I doubt this has any influence on what you observe.

    I know for a small project like this, it is silly to use the Graphics View architecture, but for the original project this is a requirement. I did however removed the Graphics View architecture as you suggested and then the QLabel updated correctly. However the original project still requires the Graphics View architecture for animations and stuff and I was under the impression that Qt 4.6.2 is a stable release.
    Ok but now we know QLabel::setText() is not the one causing problems.
    Last edited by wysota; 15th May 2010 at 18:29.
    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.


  8. #8
    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: QLabel->setText() sometimes not working

    The example above works just fine for me on Linux with Qt 4.6.2. No skipped output. Could be a Windows-ism.

  9. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLabel->setText() sometimes not working

    Quote Originally Posted by cass View Post
    I reproduced the error noticed by the original project to a smaller compilable project: ...
    ChrisW67, it works for me too, on Windows (7) and Qt 4.6.2, so that's no windows issue.

Similar Threads

  1. QLabel setText Problem
    By pmabie in forum Qt Programming
    Replies: 10
    Last Post: 1st November 2007, 23:32
  2. Problem with QLabel and setText
    By jambrek in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2007, 16:02
  3. Replies: 1
    Last Post: 26th November 2006, 09:32
  4. QLabel::setText() how to
    By freak in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2006, 17:19
  5. setText not working
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 13:05

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.