Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to display selected data from database in QTableView ?
Forum Updated to NodeBB v4.3 + New Features

How to display selected data from database in QTableView ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amative_Ashu
    wrote on 19 Apr 2018, 11:58 last edited by
    #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 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());
    }
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 19 Apr 2018, 12:29 last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5

      1/2

      19 Apr 2018, 11:58

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved