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.
1- Navigation set which is responsible of retrieving the data from database and allow the user to navigate throw data records those buttons are connected to there corresponding set of slots and ready to use.
Query All : Retrieves all data from database.
First : Navigates to the first record of the result data set.
Previous : Navigates to the previous record of the result data set.
Next : Navigates to the previous record of the result data set.
Last : Navigates to the last record of the result data set.
2- Control set, this set include New, Cancel, Delete, Find, Save and Close. The control set functionality is left for the developer to add code as needed.
The following image demonstrates both the Navigation buttons set and Control buttons set.
Files Generated By Data Widget Mapper Generator.
- A Qt project file (.pro file) for every Sohag Builder project.
- A database connection file this file contains information about database connection.
- 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 Data Widget Mapper generator, this requires to follow this article first to create items Qt / C++ project.
- Press the Data Widget Mapper generator from generate menu Generate -> Data Widget Mapper, Alternatively press the Data Widget Mapper from the Generate tool button (Press on the arrow to open the menu).
- 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, Data Widget Mapper generator uses the first class in the project in the main function.
3- A database connection file for every Sohag Builder project.
This file contains code to connect to the database .
4- Qt UI file (.ui file) for every class.
This image shows the generated form (Qt UI file in Qt Designer ).
The Qt UI file is an XML formatted file that Qt Designer UI uses to represent the widget components.
Sohag Builder's Data Widget Mapper 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 Data Widget Mapper 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.
Submits changes to the model, this slot is not connected to any signal as it's left for the developer to use it or edit it.
Revert changes, this slot is not connected to any signal as it's left for the developer to use it or edit it.
Retreives data form the database to the model, then displays the first record in the form.
Navigates between model records according to the sender button .
5- updateButtons(int row)
To enable / disable navigation buttons when no records available to navigate to.