Connecting to a database
In this recipe, we will learn how to connect our Qt 6 application to the SQL server.
How to do it…
Connecting to SQL Server in Qt is really simple:
- Open Qt Creator and create a new Qt Widgets Application project.
- Open your project file (
.pro
), add thesql
module to your project, and runqmake
like this:QT += core gui sql
- Open
mainwindow.ui
and drag seven label widgets, a combo box, and a checkbox to the canvas. Set thetext
properties of four of the labels toName:
,Age:
,Gender:
, andMarried:
. Then, set theobjectName
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:
Figure 12.10 – Setting the text properties
- Open
mainwindow.h
and add the following headers below theQMainWindow
header:#include <QMainWindow> #include <QtSql> #include <QSqlDatabase...