pr.nizar
pr.nizar

Reputation: 651

QTableView not showing all of the data from an SQLite database

I have a simple application with a QTableView and a QLineEdit and I'm using QSqlQueryModel and QSortFilterProxyModel to get the data from a table in a SQLite3 database and filter the results (with setFilterFixedString(the text from the QLineEdit)). The table in the database is 8611 rows big.

When first loaded, the tableview does not seem to be showing all of the rows in the database. When I apply a string to the filter (put some text on QLineEdit) not all of the expected results are showing; I have to delete the text from the QLineEdit and scroll down to the end of the tableview (it updates from the database?!) then the problem disappears: I have all of the results as from the database.

Is it a known bug with QTableView? How to solve this problem?

Thank you.

P.S: this is the essential parts from my application regarding this problem.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QtSql>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_searchEdit_textChanged(const QString &arg1);

private:
    Ui::MainWindow *ui;
    QSqlQueryModel *model;
    QSortFilterProxyModel *proxyModel;
};

#endif // MAINWINDOW_H

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("dpm.db");
    if (!db.open()) { }
    model = new QSqlQueryModel(this);
    model->setQuery(QString("SELECT specialite as Spécialité, dci as DCI FROM medocs"), db);
    proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(model);
    proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    ui->listeTable->setModel(proxyModel);
    ui->listeTable->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_searchEdit_textChanged(const QString &medoc)
{
    proxyModel->setFilterFixedString(medoc);
}

Upvotes: 2

Views: 1094

Answers (1)

pr.nizar
pr.nizar

Reputation: 651

It turned out Qt's SQLite driver inserts rows into the model in steps of 256 rows. I had to fetch all of the results with fetchMore().

while(model->canFetchMore()) model->fetchMore();

See Here and here.

Upvotes: 3

Related Questions