Results 1 to 10 of 10

Thread: Speed up adding widgets with setUpdatesEnabled

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Speed up adding widgets with setUpdatesEnabled

    I have a QScrollArea with a lot of QLineEdits in it. The Problem is that it takes about 2sec on my computer to add 20 new QLineEdits, Take a look at this test-window:
    Qt Code:
    1. TestWidget::TestWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QHBoxLayout* mainLayout = new QHBoxLayout;
    5. setLayout(mainLayout);
    6.  
    7. QPushButton* testButton = new QPushButton("add");
    8. connect(testButton, SIGNAL(clicked()), this, SLOT(addButtons()));
    9. mainLayout->addWidget(testButton);
    10.  
    11. QWidget* textWidget = new QWidget;
    12.  
    13. QScrollArea* scrollArea = new QScrollArea;
    14. scrollArea->setWidget(textWidget);
    15. mainLayout->addWidget(scrollArea);
    16. scrollArea->setWidgetResizable(true);
    17.  
    18. layout = new QHBoxLayout;
    19. textWidget->setLayout(layout);
    20. }
    21. void TestWidget::addButtons() {
    22. setUpdatesEnabled(false); //doesn't help
    23. for(int i=0;i<20;i++) {
    24. QLineEdit* lineEdit = new QLineEdit("foo");
    25. layout->addWidget(lineEdit);
    26. }
    27. setUpdatesEnabled(true);
    28. }
    To copy to clipboard, switch view to plain text mode 

    ...i found setUpdatesEnabled but that one doesn't have any effect. What to do else?
    Or how could I improve this else?

    niko

  2. #2
    Join Date
    Feb 2006
    Posts
    21
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Speed up adding widgets with setUpdatesEnabled

    I'm not 100% sure on this, but I think adding the controls to the scroll view causes update events to be queued up on the event queue. These updates don't actually take place until the control returns to the event loop. I.e. AFTER your TestWidget constructor returns. Therefore, the setUpdatesEnabled calls within the addButtons() method (called from the contstructor) will have no effect.

    One thing you could try is to replace the call to setUpdatesEnabled(true) with a call to QTimer::singleShot(0, this, SLOT(mySlotThatReenablesUpdates())). This timer event should go onto the event queue AFTER the update events caused by adding the buttons.

    You'll want to add the slot TestWidget::mySlotThatReenablesUpdates() that simply calls setUpdatesEnabled(true);

    Let me know if this helps.

  3. #3
    Join Date
    Jan 2006
    Posts
    105
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Speed up adding widgets with setUpdatesEnabled

    thanks for your reply, but it doesn't help

    my new code:
    Qt Code:
    1. TestWidget::TestWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QHBoxLayout* mainLayout = new QHBoxLayout;
    5. setLayout(mainLayout);
    6.  
    7. QPushButton* testButton = new QPushButton("add");
    8. connect(testButton, SIGNAL(clicked()), this, SLOT(addButtons()));
    9. mainLayout->addWidget(testButton);
    10.  
    11. QWidget* textWidget = new QWidget;
    12.  
    13. QScrollArea* scrollArea = new QScrollArea;
    14. scrollArea->setWidget(textWidget);
    15. mainLayout->addWidget(scrollArea);
    16. scrollArea->setWidgetResizable(true);
    17.  
    18. layout = new QHBoxLayout;
    19. textWidget->setLayout(layout);
    20. }
    21. void TestWidget::addButtons() {
    22. setUpdatesEnabled(false);
    23. for(int i=0;i<20;i++) {
    24. QLineEdit* lineEdit = new QLineEdit("foo");
    25. layout->addWidget(lineEdit);
    26. }
    27. QTimer::singleShot(0, this, SLOT(enableUpdates()));
    28. }
    29. void TestWidget::enableUpdates() {
    30. setUpdatesEnabled(true);
    31. }
    To copy to clipboard, switch view to plain text mode 


    ....what alternatives do i have - using a QScrollBar instead of a QScrollArea and doing the scrolling myselve? (sounds quite hard to me - does have some example code on this?)

    niko

  4. #4
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: Speed up adding widgets with setUpdatesEnabled

    have you tried to add the 20 QLineEdits to a normal widget?

    maybe the scrollarea has a bug or something..

    regards, aman..

  5. #5
    Join Date
    Jan 2006
    Posts
    105
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Speed up adding widgets with setUpdatesEnabled

    tried that allready, doesn't make much difference...

  6. #6
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: Speed up adding widgets with setUpdatesEnabled

    i've tried adding 50 lineedits to a scrollarea (i took some code from your post) and it's fast enough. i'm on a 800mhz machine..

    so the problem is probably in another place..

    i've appended my prog. but beware, it's not realy nice written..

    regards, aman..
    Attached Files Attached Files

  7. #7
    Join Date
    Jan 2006
    Posts
    105
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Speed up adding widgets with setUpdatesEnabled

    on my machine it takes about 2sec, after having 200 added it takes about 3sec.
    is it faster for you?

    I'm on Gentoo Linux 2.6.16, Xorg 7.0, nvidia-drivers - other Qt(3) apps have normal speed.

  8. #8
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default Re: Speed up adding widgets with setUpdatesEnabled

    strange..

    i'm on
    suse 10.1
    kernel: 2.6.16.13-4
    xorg 6.9.0 -> edit: and an intel gma 9xx with the intel drivers..
    qt 4.1.0

    edit:
    @1733 mhz adding 200 takes about 2 seconds..
    @800 mhz 4seconds..

    regards..
    aman..
    Last edited by aMan; 12th July 2006 at 20:26.

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

    Default Re: Speed up adding widgets with setUpdatesEnabled

    It is advised to create widgets with an appropriate parent, so the layout doesn't have to do the re-parenting. Check this thread.
    Another considerable thing could be to disable the whole layout during adding a bunch of widgets and then re-enabling afterwards.
    Quote Originally Posted by QLayout docs
    An enabled layout adjusts dynamically to changes; a disabled layout acts as if it did not exist.
    Is this any smoothier?
    Attached Files Attached Files
    J-P Nurmi

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

    Craftmansoft (6th October 2010)

  11. #10
    Join Date
    Jan 2006
    Posts
    105
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: Speed up adding widgets with setUpdatesEnabled

    thanks for that tip, the testcase is now much faster.

    however it didn't help much in my actual widget

    see here what i'm trying to do:
    http://www.qtcentre.org/forum/f-newb...lbar-2960.html

    thanks!
    niko

Similar Threads

  1. Replies: 11
    Last Post: 7th July 2006, 14:09
  2. Creating Widgets
    By hylke in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2006, 09:37

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
  •  
Qt is a trademark of The Qt Company.