Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to display selected data from database in QTableView ?



  • 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 5 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.
    0_1524138999596_Screenshot (9).png

    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());
    }
    


  • This has nothing to do with Qt, it's just about SQL

    • select top 5 eid,name,surname,salary from employeeInfo order by eid asc selects the top 5
    • select * from (select top 10 eid,name,surname,salary from employeeInfo order by eid desc) order by eid asc selects the last 10

Log in to reply