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.
1- Connect to database.
Hit connect button (first button in the toolbar).
- 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.
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.
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 that the create sql statement followed by COMMIT command to save
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.
Create a new Qt/C++ Project.
- Press on New Project button on the toolbar, alternatively select New Project from File menu.
- 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.
- In the new class wizard press Next to proceed.
- Check items table (You can use Find Table to filter tables by name).
- Press Next button.
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.
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.
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.
The next step is to use this project to generate Qt / C++ UI and code files using Qt / C++ generators.
Is used to generate the forms & code for basic opertation, Leaving the developer utilies this code depending on his requirements.
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.