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

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.

sohag builder data aware widgets qt c++ postgresql mariadb mysql

 

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

 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, 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 ).

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


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.

1- submit().
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.

2- revert().
Revert changes, this slot is not connected to any signal as it's left for the developer to use it or edit it.

3- querAll().
Retreives data form the database to the model, then displays the first record in the form.

4- changeRecord().
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.

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