QTableView::scrollTo doesn't scroll when using proxy

  • I can't get the scrollTo function to work. I'm almost certain I'm missing something trivial, but what?

    I have a QTableView object on a form, linked to a QStandardItemModel through a proxy. In this situation, the scrollTo function appears to do nothing.

    I'm using Qt 4.8.6 on RedHat 6.
    My real problem is is a rather more complex heap of code, but I've made the very bare bones bit of code below, and this reproduces the problem nicely. All the code is shown, except for the form (generated from Creator; holds only the QTableView and a button)

    If I connect the QTableView directly to the QStandardItemModel, everything works as expected. However, when I stick the QIdentityProxyModel in between them, scrolling no longer works. I suspect I have to create a derived proxy classs that overrides some default "do nothing" implementation of something, but I can't seem to find what.

    Any help greatly appreciated.



    #include "mainwindow.h"
    #include <QApplication>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        MainWindow w;
        return a.exec();


    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QIdentityProxyModel>
    #include <QStandardItemModel>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
        void fillTable(void);
        void linkup(void);
    private slots:
        void on_pushButton_clicked();
        Ui::MainWindow* m_pUi;
        QStandardItemModel m_analyzeTableModel;
        QIdentityProxyModel m_testProxy;
    #endif // MAINWINDOW_H


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    typedef QList<QStandardItem*> standardItemPList;
    MainWindow::MainWindow(QWidget *parent) :
        m_pUi(new Ui::MainWindow)
        delete m_pUi;
    void MainWindow::fillTable(void)
        QStringList headerList;
        headerList.insert(headerList.size(), "column1");
        headerList.insert(headerList.size(), "column2");
        headerList.insert(headerList.size(), "column3");
        QStandardItem *item;
        unsigned int rcursor;
        unsigned int ccursor;
        for(rcursor=1; rcursor<=40; rcursor++)
            standardItemPList row;
            for(ccursor=1; ccursor<=3; ccursor++)
                item = new QStandardItem(QString::number(100*rcursor+ccursor));
                row.insert(row.size(), item);
    void MainWindow::linkup(void)
    //works as advertised with this line
    //    m_pUi->TableView_Analysis->setModel(&m_analyzeTableModel);
    //does not work with these two
    void MainWindow::on_pushButton_clicked()
        m_pUi->TableView_Analysis->scrollTo(m_analyzeTableModel.index(25,1), QAbstractItemView::PositionAtCenter);

  • @robvr

    Found it (typical; I always find the answer five minutes after calling for help, never in the hours before..)

    In the on_pushButton_clicked function, I should have used an index from the proxy, not the underlying model.

    I knew it was something simple...

    Now all I can do is hope this post saves someone else hours of searching for the obvious..

  • Unbelievable to get help from a topic two years ago to solve the same issue.
    That's very cool, isn't it?
    Thank you so much for your "always find the answer five minutes after calling for help, never in the hours before". :D

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.