Generate Qt / C++ Forms And Code Using 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.

Files generated by Simple 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 Simple generator, this requires to follow this article first to create items Qt / C++ project.

  • Press the Simple generator from generate menu Generate -> Simple, Alternatively press the Simple from the Generate tool button (Press on the arrow to open the 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)

SQL DDL for creating items table (this structure for MariaDB, MySQL and SQLite database)

 

1- A Qt project file (.pro file) for every Sohag Builder project.
This file contains description and information 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, Simple 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 Simple 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 .ui 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 Simple 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- newRecord().
Initialize the form to receive a new record data by clearing all widgets data (Items related to database table columns), starting a new transaction and setting variables to indicate a new record and unchanged record status.

2- save().
a- Save method checks the record status if changed it will insert or update the record depending on the operation mode.
b- Commits changes to the database.
c- Changes the mode to suit the next operation.
If the operation was create or edit the next operation will be edit which allows the user to edit the data and the next save will execute the update method, If the operation was remove (Deleting record) the new operation will be create new record which prepares the form to receive a new record.
d- If save operation succeeded a new database transaction starts otherwise an error will be raised.

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

4- setStatusChanged().
Changes the flag variable status to the value ChangedStatus which indicates that the form data has been changed.

5- error(QString code, QString errorText, int operation)
Used to show a message box that indicates the error data.

6-  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 .

7-  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 responsible 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.

Note

In case of using composite primary key, you have to edit the update, delete and query method's conditions to add all the columns in the composite primary key

8- 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.

9- 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.

10- 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 Developer 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 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).

11- 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 Developer 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 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).

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