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

QTreeView does not scroll to selected item

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P2: Important
    • Resolution: Unresolved
    • Affects Version/s: 5.15.2, 6.1.3
    • Fix Version/s: None
    • Component/s: Widgets: Itemviews
    • Labels:
      None
    • Platform/s:
      Linux/X11

      Description

      QTreeView::scrollTo uses the scroll bar's pageStep as count of how many items are visible in the viewport. It uses that and the index of the first visible item to check whether the item is already in view. The pageStep however is not dependent on the actually visible items in the view, but instead is calculated by taking as many items from the end of the model that would fit into the view.

      The result is that if the end of the model has smaller items, pageStep is bigger than it should be, because more of the smaller items fit into view. Thus it thinks the target item fits, and does not scroll, even if the item is partially or completely out of view.

      Here a reproducer, just press arrow down on the keyboard until the selected item is no longer visible:

      #include <QApplication>
      #include <QTreeView>
      #include <QStandardItemModel>
      #include <QStandardItem>
      
      int main(int argc, char *argv[])
      {
              QApplication app(argc, argv);
              QTreeView tv;
              QStandardItemModel sim;
      
              // Add 20 items with 20px height and 20 items with 10px height
              for(int i = 0; i < 40; ++i) {
                      auto *item = new QStandardItem(QStringLiteral("Item %1").arg(i));
                      item->setSizeHint(QSize(100, i < 20 ? 20 : 10));
                      sim.appendRow(item);
              }
      
              tv.setModel(&sim);
              tv.show();
              return app.exec();
      }
      

      A workaround is to use QAbstractItemView::ScrollPerPixel instead of QAbstractItemView::ScrollPerItem.

        Attachments

        For Gerrit Dashboard: QTBUG-96980
        # Subject Branch Project Status CR V

          Activity

            People

            Assignee:
            qt.team.quick.subscriptions Qt Quick and Widgets Team
            Reporter:
            vogtinator Fabian Vogt
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There is 1 open Gerrit change