Generate Qt / C++ Forms And Code Using Basic Generator

Basic generator generates UI and Qt / C++ code that executes the SQL DML, It uses Qt low level SQL API to interact with the database.

Files generated by Basic Generator.

  • A Qt project file (.pro file) for every Sohag Builder project.
  • A C++ main file (main.cpp file contains main function) for every Sohag Builder project.
  • Qt UI file (.ui file) for every class or database table or view.
  • C++ header file (.h) for every class or database table or view.
  • C++ implementation file (.cpp file) for every class or database table or view.

Let's generate the Qt / C++ forms & classes using Basic gnerator, this requires to follow this article first to create items Qt / C++ project.

  • Press the Basic generator from the Generate tool button (Press on the arrow to open the menu), Alternatively press the Basic from generate menu.

 sohag builder project generate qt c++ class code and forms (ui files) postgresql mariadb mysql

  • To explore the generated files, Press on Project Menu -> Open Project Location.

In the next sections you will find explanation for every file and its main contents .
The example is built on database table items.
SQL DDL for creating items table (this structure for PostgreSQL database)

1- A Qt project file (.pro file) for every Sohag Builder project.
This file contains description and informations that qmake uses to generate the make file for the project

2- A C++ main file (main.cpp file contains main function) for every Sohag Builder project.
This file contains the main function, Basic generator uses the first class in the project in the main function.

3- Qt UI file (.ui file) for every class.

This image shows the generated form (Qt UI file in Qt Designer ).

Sohag Builder generated form (Qt UI file in Qt Designer ) for  postgresql, mariadb, mysql or sqlite database.


The Qt UI file is an XML formatted file that Qt Designer UI uses to represent the widget components.
Sohag Builder's Basic generator generate this file for every class in the project with following specifications
a- The file name is the same as class name but with all lower case letters with .h extension at the end..
b- A label is generated corresponding to every column in the database table or view (the label name is the same as table column name with additional (L) at the end).
c- A widget is generated according to the user selection in the class wizard with the same name generated by the class wizard .
d- The Basic generator adds the class name, widget name and window title the same as class name.
e- All widgets and corresponding labels are put in a QGroupBox container and laid out in a QGridLayout.
 

4- C++ header file (.h) for every class.
This is the class header file & it contains the definitions.

5- C++ implementation file (.cpp file) for every class.
This file contains the implementation of the class code
The file name is the same as class name but all letters are in lower case with .cpp extension at the end.

1- clear().
Clear method clears all widgets to empty values you may use it after delete, cancel and initialization operations.

2- toVarinat(QString string).
This method converts empty Qstring value to null to store it as null in the database since empty QString is a different value than null .

3- toString(QVariant value)
Qt uses the Qvariant class as a container that receives all database data types and then these data types can be casted according to the widgets that will receive them
, Widgets like Qlabel or QlinEdit displays Qstring (text data) but the returned value from database can be of numeric data types and if this value is null the QString value may be 0 Zero (This problem does'nt exist on all Qt versions)
so using toString method returns an empty QString if the database value is null otherwise it will return QString that represents the numeric database value with its corresponding format in Qt.

The coming section include the methods that are responsable of executing the DML SQL operations (Insert, Update, Delete and Query)
And all the methods uses prepared quires achieve both best database performance and ensure security by preventing sql injections.

4- insert()
Insert method uses prepared query to insert a new record in the database table or view (Note that inserting data into view depends on a several parameters related to the view and the database).
Binding values using positional binding then executing the query and finally the method returns true if SQL query executed without errors otherwise it will return false.

5- update()
Update method uses prepared query to update record in the database table or view (Note that updating data in a view depends on a several aspects related to the view and the database).
Binding values using positional binding then executing the query and finally the method returns true if SQL query executed without errors otherwise it will return false.
Note that Sohag builder will detect the primary key column automatically and add a condition in the update statement according to the primary key to ensure that only the required record will be updated, If the table doesn't have a primary key or have composite primary key you have to edit the update statement to add the condition (It's always good practice to add a primary key in you database table design).

6- remove()
Remove method uses prepared query to delete a record from the database table or view (Note that deleting data from a view depends on a several aspects related to the view and the database).
Binding primary key value using positional binding then executing the query and finally the method returns true if SQL query executed without errors otherwise it will return false.
Note that Sohag Builder will detect the primary key column automatically and add a condition in the delete statement according to the primary key to ensure that only the required record will be removed, If the table doesn't have a primary key or have composite primary key you have to edit the delete statement to add the condition (It's always good practice to add a primary key in you database table design).

7- query()
Query method is used to retrieve a database table or view record and displays the returned values to the widgets corresponding to database columns.
Also this method uses the primary key to retrieve the required record if the condition is omitted or Sohag Builder didn't find a primary key the first record only of returned data result will be displayed
This function returns true if SQL query executed without errors otherwise it will return false.

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.
18 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Verion
v1.0