Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved How to display selected data from database in QTableView ?

    General and Desktop
    2
    2
    943
    Loading More Posts
    • 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
      Amative_Ashu last edited by

      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 Reply Quote 0
      • V
        VRonin last edited by

        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 Reply Quote 5
        • First post
          Last post