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 ...
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 ...
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
QListWidget::addItem() and the problem is that ... doesn't update the view
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:
for (int i = 0; i < 1000; ++i) listWidget->addItem(something); // each item added separately, view gets informed thousand times, once per insertionTo copy to clipboard, switch view to plain text modeQt Code:
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 totalTo copy to clipboard, switch view to plain text mode
J-P Nurmi
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
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:
// a busy loop for (...) { // do something // let the application process its events }To copy to clipboard, switch view to plain text mode
J-P Nurmi
vlg789 (24th September 2007)
Thanks man,
this is what i was looking for.
Bookmarks