Important: Please read the 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);
        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);
        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


    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)
        QModelIndex index = table->model()->index(50, 0);
        table->scrollTo(index, QAbstractItemView::PositionAtBottom);

    scrollTo_bottom.png )

Log in to reply