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.
Rob
main.cpp:
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); w.fillTable(); w.linkup(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QIdentityProxyModel> #include <QStandardItemModel> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void fillTable(void); void linkup(void); private slots: void on_pushButton_clicked(); private: Ui::MainWindow* m_pUi; QStandardItemModel m_analyzeTableModel; QIdentityProxyModel m_testProxy; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" typedef QList<QStandardItem*> standardItemPList; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), m_pUi(new Ui::MainWindow) { m_pUi->setupUi(this); } MainWindow::~MainWindow() { delete m_pUi; } void MainWindow::fillTable(void) { m_analyzeTableModel.clear(); QStringList headerList; headerList.insert(headerList.size(), "column1"); headerList.insert(headerList.size(), "column2"); headerList.insert(headerList.size(), "column3"); m_analyzeTableModel.setHorizontalHeaderLabels(headerList); 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); } m_analyzeTableModel.appendRow(row); } } void MainWindow::linkup(void) { //works as advertised with this line // m_pUi->TableView_Analysis->setModel(&m_analyzeTableModel); //does not work with these two m_testProxy.setSourceModel(&m_analyzeTableModel); m_pUi->TableView_Analysis->setModel(&m_testProxy); } void MainWindow::on_pushButton_clicked() { m_pUi->TableView_Analysis->setCurrentIndex(m_analyzeTableModel.index(25,1)); m_pUi->TableView_Analysis->scrollTo(m_analyzeTableModel.index(25,1), QAbstractItemView::PositionAtCenter); }
-
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..