-1

I am a beginner in Qt programming and I am working on a project in which I am using SQLite database.

I have 20 rows in my database and I just want to show last five rows in QTableView when I press show button first time. If I then press the show button a second time, the last 10 rows should be shown from the database.

But I don't have any idea what codes I should write. Please help me.

look at this image I just want to display last five rows when I click show button first time and then I want to display 10 last rows when I click show button the second time.

Thank you in advance.

Here is my code to show the data from SQLite database into QTableView.

void SecondWindow::on_pushButton_show_clicked()
{
    MainWindow conn;
    QSqlQueryModel *model = new QSqlQueryModel();
    conn.openConn();
    QSqlQuery *qry = new QSqlQuery(conn.mydb);
    qry->prepare("select eid,name,surname,salary from employeeInfo");
    qry->exec();
    model->setQuery(*qry);
    ui->tableView->setModel(model);
    conn.closeConn();
    qDebug() << (model->rowCount());
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

2 Answers2

0

You are looking for the LIMIT keyword in your SQL query, e.g.:

"select eid,name,surname,salary from employeeInfo LIMIT 10 OFFSET 10"

will return 10 rows after the first 10.

Sergio Monteleone
  • 2,776
  • 9
  • 21
0

if you don't want to send SQL query every time, but just change number of displayed elements, you may write QAbstractProxyModel-based model class. You'll only need to override mapFromSource, mapToSource and rowCount methods

Andrei R.
  • 2,374
  • 1
  • 13
  • 27