Results 1 to 7 of 7

Thread: QListWidget::addItem and display immediately

  1. #1
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QListWidget::addItem and display immediately

    i have a for that prints some output in a QListWidget. There are many iterations and each iteration takes a while... The problem is that QListWidget prints data at the end of the iteration. The question is if i can avoid QThread ...

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

    Default Re: QListWidget::addItem and display immediately

    Are you using QListWidget::addItem() or QListWidget::addItems()? The latter should be remarkably faster because there is no need to update the view after adding every single item.
    J-P Nurmi

  3. #3
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget::addItem and display immediately

    QListWidget::addItem() and the problem is that ... doesn't update the view

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

    Default Re: QListWidget::addItem and display immediately

    Yes, it doesn't update because of the busy loop which blocks the event loop. Paint events get scheduled but you are not giving the application any chance to actually deliver them. Behind the curtains the view gets informed about each and every insertion. This is most likely one of main reasons why it takes so long to insert items one by one. Could you give QListWidget::addItems() a try? If it's still too slow, we'll look for other possibilities.

    Qt Code:
    1. for (int i = 0; i < 1000; ++i)
    2. listWidget->addItem(something); // each item added separately, view gets informed thousand times, once per insertion
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. for (int i = 0; i < 1000; ++i)
    2. items += something;
    3. listWidget->addItems(items); // everything gets inserted at one go, view gets informed only once in total
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget::addItem and display immediately

    First of all thanks for the quick answer.

    The main reasons why it takes so long to insert items are some operations that take place in background. The listview must keep user informed with the progress, so the problem is not to optimize insertion of items, but to update the listbox immediately.

    The only problem i have is that QThread needs a run() and this makes my design more odd... The question is if i can avoid QThread ...

    Thanks

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

    Default Re: QListWidget::addItem and display immediately

    One way would be to call QCoreApplication::processEvents() every once in a while to let the application process its events. This will make the overall process a bit longer, though:
    Qt Code:
    1. // a busy loop
    2. for (...)
    3. {
    4. // do something
    5.  
    6. // let the application process its events
    7. QCoreApplication::processEvents();
    8. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    vlg789 (24th September 2007)

  8. #7
    Join Date
    Sep 2007
    Posts
    7
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: QListWidget::addItem and display immediately

    Thanks man,
    this is what i was looking for.

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.