Book Image

Qt5 C++ GUI Programming Cookbook

By : Eng
Book Image

Qt5 C++ GUI Programming Cookbook

By: Eng

Overview of this book

With the advancement of computer technology, the software market is exploding with tons of software choices for the user, making their expectations higher in terms of functionality and the look and feel of the application. Therefore, improving the visual quality of your application is vital in order to overcome the market competition and stand out from the crowd. This book will teach you how to develop functional and appealing software using Qt5 through multiple projects that are interesting and fun. This book covers a variety of topics such as look-and-feel customization, GUI animation, graphics rendering, implementing Google Maps, and more. You will learn tons of useful information, and enjoy the process of working on the creative projects provided in this book
Table of Contents (11 chapters)
10
Index

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 mainwindow.h and add the following headers below the QMainWindow header:

    #include <QMainWindow>
    #include <QtSql>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QDebug>
    
  5. Then, open up mainwindow.cpp and insert the...