Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Scrolling hint PositionAtBottom not working as expected



  • Using a table view or a table widget, I cannot seem to scrollTo an item index such that the row is placed at the bottom of the viewport. Is there a workaround I can use?

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        const int kRows = 100;
        QHBoxLayout *layout = new QHBoxLayout();
        QWidget *central = new QWidget();
        table = new QTableWidget(kRows, 1);
        table->verticalHeader()->setVisible(false);
        QTableWidgetItem *header = new QTableWidgetItem("ROWS");
        table->setHorizontalHeaderItem(0, header);
        for (int i = 0; i < kRows; ++i) {
            QTableWidgetItem *item = new QTableWidgetItem(QString("row %1").arg(i));
            table->setItem(i, 0, item);
        }
        layout->addWidget(table);
        central->setLayout(layout);
        setCentralWidget(central);
        setMinimumHeight(305);
        table->resizeRowsToContents();
        QModelIndex index = table->model()->index(50, 0);
        // this line below does not move row 50 to the bottom
        table->scrollTo(index, QAbstractItemView::PositionAtBottom);
    }
    

    Positioning an item using the top hint works as expected, i.e. that row appears at the top of the viewport.

    table->scrollTo(index, QAbstractItemView::PositionAtTop);
    

  • Lifetime Qt Champion

    Hi,

    What if you trigger the scrollTo after your widget has been shown ?



  • @SGaist Yes, much better, thanks! I should have had that intution on visibility -- live and learn. Marking as resolved. -Phil

    void MainWindow::showEvent(QShowEvent *event)
    {
        QMainWindow::showEvent(event);
        QModelIndex index = table->model()->index(50, 0);
        table->scrollTo(index, QAbstractItemView::PositionAtBottom);
    }
    

    scrollTo_bottom.png )


Log in to reply