Important: Please read the Qt Code of Conduct -

Adding items to ListWidget is slow

  • Ok, not so slow for a normal application, but I am developing an appllication, that, depending on user actions, a listwidget is being filled. The thing is that the entries are far too many and may be up to 200-300 per sec.
    It works more than OK if, when I add the items I use the following code:
    @ui->listWidget->additem("MyNewItem text here");@

    BUT I want the items to be editable with double click etc, so I have to fix the flags to each of them as far as I know. So, the above one-liner becomes this:
    @QListWidgetitem *item = new QListWidgetItem;
    item->setText("MyNewItem text here");
    item->setFlags(item->flags() | Qt::ItemIsEditable);

    This adds one or two milliseconds more to the addition of each of my items, and, considering how many items I'm adding per second, the program slows down a lot.

    Is there any way to solve this problem? Is there any way to tell the listwidget, if it has editTriggers, then all the items added to it to be editable by default so as not to have to do it for each individual item?

  • Moderators

    Have you considered using a QListView instead of a QListWidget in order to decouple the model from the view?

  • [quote author="mlong" date="1347293588"]Have you considered using a QListView instead of a QListWidget in order to decouple the model from the view?[/quote]

    Thanks for this! Will just the conversion from QListWidget to QListView make the whole thing run faster? What will I gain?

  • For one, you gain the option to insert multiple rows in a single call. Also, you don't have to create actual objects for each of your cells, which eliminates the repeated heap memory allocation. Memory allocations are quite slow, so that might help.

    However, based on this alone, there is no definitive way to say if you are going to gain speed. At least, you will gain more vectors for optimization. To find out what speed increases you get, there is just one answer: benchmark. The same goes for your current solution by the way. You should analyze what exactly takes the most time in your current situation, and focus your efforts on that. Personally, I doubt that the flag setting is the issue. I expect the constant re-layouting and repainting of the list to be at least an order of magnitude more expensive.

Log in to reply