PDA

View Full Version : QListWidget::addItem and display immediately



vlg789
24th September 2007, 13:52
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 ...

jpn
24th September 2007, 14:02
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.

vlg789
24th September 2007, 14:06
QListWidget::addItem() and the problem is that ... doesn't update the view :)

jpn
24th September 2007, 14:18
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.



for (int i = 0; i < 1000; ++i)
listWidget->addItem(something); // each item added separately, view gets informed thousand times, once per insertion



QStringList items;
for (int i = 0; i < 1000; ++i)
items += something;
listWidget->addItems(items); // everything gets inserted at one go, view gets informed only once in total

vlg789
24th September 2007, 14:40
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

jpn
24th September 2007, 14:59
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:


// a busy loop
for (...)
{
// do something

// let the application process its events
QCoreApplication::processEvents();
}

vlg789
24th September 2007, 15:30
Thanks man,
this is what i was looking for.