Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-401

The performance of a QListWidget with a lot of non-consecutive selected items is very bad compared to QListBox in Qt 3

    XMLWordPrintable

Details

    Description

      The following apps shows the differences:

      Qt 4
      #include <QListWidget>
      #include <QWidget>
      #include <QString>
      #include <QTime>
      #include <QApplication>
      #include <QListWidgetItem>
      #include <QShortcut>
      #include <QKeySequence>
      static int itemCount = 50000;
      static int selectAmount = 5000;
      class ListWidget : public QListWidget
      {
      Q_OBJECT
      public:
      ListWidget(QWidget *parent = 0)
      : QListWidget(parent)
      {
      for (int i=0; i<itemCount; ++i)

      { new QListWidgetItem(QString("Item number %1").arg(i), this); }

      new QShortcut(QKeySequence(Qt::Key_F2), this, SLOT(selectRandomAmountOfItems()));
      setSelectionMode(ExtendedSelection);
      }
      public slots:
      void selectRandomAmountOfItems()
      {
      clearSelection();
      QTime timer;
      timer.start();
      int i = 0;
      do {
      QListWidgetItem *it = item(rand() % itemCount);
      if (!isItemSelected(it))

      { ++i; it->setSelected(true); }

      } while (i < selectAmount);
      qDebug("%d elapsed", timer.elapsed());
      }
      };

      #include "main.moc"

      int main(int argc, char **argv)
      {
      if (argc > 1)

      { itemCount = atoi(argv[1]); if (argc > 2) selectAmount = atoi(argv[2]); }

      QApplication a(argc, argv);
      ListWidget w;
      w.show();
      return a.exec();
      }

      Qt 3:

      #include <qapplication.h>
      #include <qlistbox.h>
      #include <qwidget.h>
      #include <qstring.h>
      #include <qaccel.h>
      #include <qdatetime.h>

      static int itemCount = 50000;
      static int selectAmount = 5000;
      class ListWidget : public QListBox
      {
      Q_OBJECT
      public:
      ListWidget(QWidget *parent = 0)
      : QListBox(parent)
      {
      for (int i=0; i<itemCount; ++i) { new QListBoxText(this, QString("Item number %1").arg(i)); }
      QAccel *accel = new QAccel(this);
      accel->connectItem(accel->insertItem(Key_F2),
      this, SLOT(selectRandomAmountOfItems()));
      setSelectionMode(Extended);
      }
      public slots:
      void selectRandomAmountOfItems()
      {
      clearSelection();
      QTime timer;
      timer.start();
      int i = 0;
      do {
      const int index = rand() % itemCount;
      if (!isSelected(index)) { ++i; setSelected(index, true); }
      } while (i < selectAmount);
      qDebug("%d elapsed", timer.elapsed());
      }
      };

      #include "main.moc"


      int main(int argc, char **argv)
      {
      if (argc > 1) { itemCount = atoi(argv[1]); if (argc > 2) selectAmount = atoi(argv[2]); }

      QApplication a(argc, argv);
      ListWidget w;
      w.show();
      return a.exec();
      }

      To invoke a random selection press F2.

      Started like this "./test 5000 1000" I get these results:

      Qt 3: 48 elapsed
      Qt 4: 2299 elapsed

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bjnilsen Bjørn Erik Nilsen
            rve Anders Bakken
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes