-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Qt5 C++ GUI Programming Cookbook
By :
In this recipe, we will learn how to connect to our SQL database using Qt's SQL module.
Connecting to SQL server in Qt is really simple:
First of all, open up Qt Creator and create a new Qt Widgets Application project.
Open up your project file (.pro) and add the SQL module to your project, like so:
QT += core gui sql
Next, open up mainwindow.ui and drag seven label widgets, a combo box, and a checkbox to the canvas. Set the text properties of four of the labels to Name:, Age:, Gender:, and Married:. Then, set the objectName properties of the rest to name, age, gender, and married. There is no need to set the object name for the previous four labels because they're for display purposes only:

After that, open up mainwindow.h and add the following headers below the QMainWindow header:
#include <QMainWindow> #include <QtSql> #include <QSqlDatabase> #include <QSqlQuery> #include <QDebug>
Then, open up mainwindow.cpp and insert the...