How to put SQL table data on QTableView

  c++, qt, qt5.4, qtableview

this code gives me the exact cell count with tables heders. but not table data. is there any way to get SQL data to Qtableview?

code :

if(db.open()){
    qDebug() << "Opened!!";
    QSqlQuery *query = new QSqlQuery(db);
    query->prepare("SELECT COUNT(Id) AS Student_Count, Gender  FROM Students GROUP BY Gender;");
    query->exec();
    QSqlQueryModel* model = new QSqlQueryModel();
    ui->tableView->setModel(model);
    model->setQuery(*query);
    qDebug() << model->rowCount();


    db.close();
}else{
    qDebug() << "Error : " << db.lastError().text();
}

Output : output

Source: Windows Questions C++

LEAVE A COMMENT