In the previous section, we saw how to access the database using the model as an abstraction. Now, we shall try to link it with a model for display. Using the code listing from the previous section, modify main.cpp to appear as follows:
#include <QApplication>#include <QtSql>#include <QVBoxLayout>#include <QPushButton>#include <QDebug>#include <Qt>#include <QTableView>#include <QHeaderView>int main(int argc, char *argv[]){ QApplication app(argc, argv); // Setup db connection QSqlDatabase db_conn = QSqlDatabase::addDatabase("QMYSQL", "contact_db"); db_conn.setHostName("127.0.0.1"); db_conn.setDatabaseName("contact_db"); db_conn.setUserName("root"); db_conn.setPassword(""); db_conn.setPort(3306); // Error checks if (!db_conn.open()) ...