Connecting to a database

In this recipe, we will learn how to connect to our SQL database using Qt's SQL module.

How to do it…

Connecting to SQL server in Qt is really simple:

  1. First of all, open up Qt Creator and create a new Qt Widgets Application project.
  2. Open up your project file (.pro) and add the SQL module to your project, like so:
    QT += core gui sql
  3. 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:
  4. After that, open up ...

Get Qt5 C++ GUI Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.