Create Qt / C++ Project

Creating a Qt/C++ project,  to generate  Qt / C++ code and UI files to perform CURD operations (Create, Update, Read and Delete database table records) to database table or view.

This tutorial uses PostgreSQL database but you can accomplish the same task with other supported database management system (MariaDB, MySQL and SQLite).

  • Connect to database.

  • Define database table structure.

  • Create the table using SQL Tool.

  • Create Project.

 

1- Connect to database.

Hit connect button (first button in the toolbar).

Sohag Builder connect to database

  • From the connection dialog choose PostgreSQL  from the database type list (PostgreSQL is default selection ).
  • Add the database credentials  (you may leave host and port empty, if you are using the local host and default database port) .
  • Press the Connect button to connect to the database.

sohag Builder postgresql connection to database

2- Define database table structure

The items table is constructed from 5 columns (item_id, item_name, cost, sales_price, notes  to store basic items data.

Follow the steps bellow to create the item table using Sohag Builder SQL Tool.

Tip

Sohag Builder supports creating database table by wizard
Follow this tutorial to create a new table by wizard

  • Open a new SQL Tool.
  • Copy the item table create statement and paste it to the SQL Tool
  • Press Execute button to execute the code.


CREATE TABLE items (
	item_id	INTEGER PRIMARY KEY,
	item_name	TEXT NOT NULL,
	cost	DOUBLE PRECISION,
	sales_price	DOUBLE PRECISION,
	notes	TEXT
);
COMMIT;

Note

Note that the create sql statement followed by COMMIT command to save

Sohag Builder Create PostgreSQL database table From SQL Tool

Now we need to reload the database info pane to display the newly created table.

  • Open the context menu from database info pane either by using right mouse button or from the keyboard context menu button.
  • From the menu press on Refresh.

sohag builder reload database metadata

Create a new Qt/C++ Project.

  • Press on New Project button on the toolbar, alternatively select New Project from File menu.

Sohag Builder create a Qt c++ database application postgresql mariadb mysql sqlite

  • Press Next to proceed to the next page.
  • From Project Type list select Qt/C++  Application (This is the default selection).
  • Write items in the Project Name.
  • Set the project path, or leave the default location (documents). 
  • Press Finish to proceed with the class wizard.

Sohag Builder create a Qt c++ database application postgresql mariadb mysql sqlite

  • In the new class wizard press Next to proceed.

Sohag Builder create a Qt c++ database application postgresql mariadb mysql sqlite

  • Check items table (You can use Find Table to filter tables by name).
  • Press Next button.

Sohag Builder create a Qt c++ database application postgresql mariadb mysql sqlite, database table or view

This page allows us to specify which columns will be used and fields corresponding to the specified columns.

  • To use column check the column name.

To change a label.

  • Double click the label in the grid then edit it.

To change a widget.

  • select the widget you want to change in the grid.
  • select the new widget from the Set Widget To list.
  • Press Set Widget button.

To select / deselect all columns.

  • Set Select all Columns checked / unchecked.

To set all widgets to QLineEdit.

  • Set Select all widgets to QLineEdit.

For this tutorial

  • Set Select all columns checked.
  • Press Next button to proceed.

Sohag Builder create a Qt c++ database application postgresql mariadb mysql sqlite, database table or view columns, widgets, labels

In this page you can do the following.

  • Change the class name.
  • Change base class QWidget / QDialog.
  • Set master detail relationship.

For this tutorial

  • Press Finish button to proceed.

Sohag Builder create a Qt c++ database application postgresql mariadb mysql sqlite, class information, QDialog, QWidget, Master detail form

Tip

You can add more classes as you want, press New Class button and follow same steps to add a new class.

Here is the project summery

This page include all project data and classes data.

  • Press Save button on the toolbar to save the project.

Sohag Builder create a Qt c++ database application postgresql mariadb mysql sqlite, Project summery

The next step is to use this project to generate Qt / C++ UI and code files using Qt / C++ generators.

Basic Generator.

Is used to generate the forms & code for basic opertation, Leaving the developer utilies this code depending on his requirements.

Simple generator.

Simple generator generates UI and Qt / C++ code to execute the SQL DML, It uses Qt low level SQL API to interact with the database. The generated forms and code also contains a set of control buttons that are connected to corresponding set of slots to execute query, new, delete, save and close operations. So the generated code is ready to use and may not need any changes from the developer.

Data Widget Mapper generator.

Data Widget Mapper generator generates UI and Qt / C++ code that utilize QDataWidgetMapper class to create data aware widgets by mapping  them to sections of an item model. The generated forms contains two sets of control buttons.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
3 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Verion
v1.0