Results 1 to 5 of 5

Thread: refreshing labels

  1. #1
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default refreshing labels

    Hi,

    I was wondering why my label does not automatically update while this is running:

    Qt Code:
    1. for (int kk=1; kk<100; kk++)
    2. {
    3. //main code is here
    4. Label->setNum(kk);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Label is a QLabel declared elsewhere

    I'd like to see the numbers counted between 1 and 99 but the only number I see is 99 (when the program is complete). Why don't I see other numbers sequentially on the screen. Is it because labels can be refreshed only when control is returned to main()?
    Can labels be refreshed while another function is running?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: refreshing labels

    You set the numbers too fast. The label doesn't get the chance to update.
    You should use a QTimer with an interval of 100-200ms. Connect its timeout signal to a slot in which you update the label with a new value. Then you will be able to see something.

  3. The following user says thank you to marcel for this useful post:

    ht1 (29th December 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    47
    Thanks
    34
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: refreshing labels

    Thanks Marcel, but I think something else than speed is the problem.
    The code that I designated as (//main code here) is very long and takes a long time to compile. The labels have about a minute to refresh. Maybe the labels don't refresh because the processor is 100% busy with doing the math?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: refreshing labels

    Quote Originally Posted by ht1 View Post
    Maybe the labels don't refresh because the processor is 100% busy with doing the math?
    Yes, you block the Qt's event loop and Qt can't redraw anything or even process user's input. You can avoid this by invoking QCoreApplication::processEvents() from time to time or using QTimer to drive your loop. A third option is to move those computations to a thread.

  6. The following user says thank you to jacek for this useful post:

    ht1 (29th December 2007)

  7. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: refreshing labels

    In other words, events don't get processed during a busy loop. When you change the contents of a label, a few events get scheduled (to update the label). But no event reaches its receiver until you return the control to the event loop. One simple but a bit ugly solution is to force the application to process its events:
    Qt Code:
    1. for (int kk=1; kk<100; kk++)
    2. {
    3. //main code is here
    4. Label->setNum(kk);
    5. QApplication::processEvents(); // <--
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    ht1 (29th December 2007)

Similar Threads

  1. Tick Labels for Slider
    By jcraig in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 18:21
  2. QSlider with a custom set of labels for the tick marks?
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2007, 17:05
  3. setting QTreeWidget vertical labels
    By hyling in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2007, 19:53
  4. Refreshing problem
    By jaime in forum Qt Programming
    Replies: 3
    Last Post: 24th August 2006, 23:09
  5. Replies: 1
    Last Post: 23rd August 2006, 19:02

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.