Sohag Developer PostgreSQL Database Applications

Build Powerful Robust Database Applications

sohag developer code (Basic Template)

Wed, 07/05/2017 - 11:11 - amr

Sohag Developer For PostgreSQL generates the Qt project files according to three templates theses templates are
1- Basic
2- Simple
3- Sohag Save Point

You can find them in the Generate button on the toolbar or the Generate menu

Note that Qt project file (.pro file) and main function (main.cpp) file are identical in the three templates the differnce is only in the Qt Designer form (.ui file that contains the gui widgets) and the generated class code that contains the SQL DML methods to mainupalte the database table data.
View Qt Project generated files for more information.

In this topic we will explore the generated project files of the Basic template.

Suppose that we have the following Postgesql database table.

Database Table Structure


CREATE TABLE public.employees
(
  employee_id serial NOT NULL,
  employee_name character varying(500),
  gender integer,
  birth_date date,
  department_id integer,
  job_id integer,
  CONSTRAINT employees_pkey PRIMARY KEY (employee_id)
)
WITH (
  OIDS=FALSE
);

The generated Qt Designer Form (.ui File)

Qt Designer Form (.ui File)
The generated Qt Designer Form will contain two widgets for each selected column from the database tabel
One Widget is a QLabel widget and it will contain the label for the Other widget which will conatins the column data that will be mainupalted.
The generated Qt Designer Form is laid out in a QGridLayout.
The following image shows the generated Qt Designer form (,ui file) opend using Qt Creator.

The generated Qt/C++ class header file .h
The header file of the basic template class contains definitions for the class methods.

Basic Template Class Header


#ifndef EMPLOYEES_H
#define EMPLOYEES_H

#include 

namespace Ui {
class Employees;
}

class Employees : public QWidget
{
    Q_OBJECT

public:
    explicit Employees(QWidget *parent = 0);
    ~Employees();

private:
    Ui::Employees *ui;
    bool insert();
	bool update();
	bool remove();
	bool query();
    QVariant toVarinat(QString string);
    QString toString(QVariant value);
private slots:
    void clear();
};

#endif // EMPLOYEES_H

The generated Qt/C++ class implemenation file .cpp is

Basic Template Class Implementation


#include "employees.h"
#include "ui_employees.h"
#include 
#include 

Employees::Employees(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Employees)
{
    ui->setupUi(this);
}

Employees::~Employees()
{
    delete ui;
}


void Employees::clear()
{
	ui->employee_id->clear();
	ui->employee_name->clear();
	ui->gender->clear();
	ui->birth_date->clear();
	
}

QVariant Employees::toVarinat(QString string)
{
    if (string.isEmpty()){
        return QVariant();
    }
    return QVariant(string);
}

QString Employees::toString(QVariant value)
{
	int valueType=value.type();
    if(value.isNull()){
        return QString();
    }else if(valueType==QMetaType::Int){
        return QString::number(value.toInt());
    }else if(valueType==QMetaType::UInt){
        return QString::number(value.toUInt());
    }else if(valueType==QMetaType::Double){
        return QString::number(value.toDouble());
    }else if(valueType==QMetaType::Long){
        return QString::number(value.toLongLong());
    }else if(valueType==QMetaType::LongLong){
        return QString::number(value.toLongLong());
    }else if(valueType==QMetaType::Short){
        return QString::number(value.toInt());
    }else if(valueType==QMetaType::ULong){
        return QString::number(value.toULongLong());
    }else if(valueType==QMetaType::ULongLong){
        return QString::number(value.toULongLong());
    }else if(valueType==QMetaType::UShort){
        return QString::number(value.toUInt());
    }else if(valueType==QMetaType::Float){
        return QString::number(value.toFloat());
    }
    return value.toString();
}

bool Employees::insert()
{
    QSqlQuery query;
    query.prepare("insert into public.employees(employee_id,employee_name,gender,birth_date) values (?,?,?,?)");
	query.addBindValue(toVarinat(ui->employee_id->text()));
	query.addBindValue(toVarinat(ui->employee_name->text()));
	query.addBindValue(toVarinat(ui->gender->text()));
	query.addBindValue(toVarinat(ui->birth_date->text()));


    query.exec();
    if(query.lastError().isValid()){

        return false;
    }
    return true;
}


bool Employees::update()
{
    QSqlQuery query;
    query.prepare("UPDATE public.employees SET employee_id=?,employee_name=?,gender=?,birth_date=? WHERE employee_id=?");
	query.addBindValue(toVarinat(ui->employee_id->text()));
	query.addBindValue(toVarinat(ui->employee_name->text()));
	query.addBindValue(toVarinat(ui->gender->text()));
	query.addBindValue(toVarinat(ui->birth_date->text()));


	query.addBindValue(ui->employee_id->text());


    query.exec();
    if(query.lastError().isValid()){

        return false;
    }
    return true;
}




bool Employees::remove()
{
    QSqlQuery query;
    query.prepare("delete from public.employees WHERE employee_id=?");

	query.addBindValue(ui->employee_id->text());


    query.exec();
    if(query.lastError().isValid()){

        return false;
    }
    return true;
}


bool Employees::query()
{
    QSqlQuery query;
    query.prepare("select employee_id,employee_name,gender,birth_date from public.employees WHERE employee_id=?");

	query.addBindValue(ui->employee_id->text());


    query.exec();
    if(query.lastError().isValid()){

        return false;
    }
    query.first();
	ui->employee_id->setText(toString(query.value(0)));
	ui->employee_name->setText(toString(query.value(1)));
	ui->gender->setText(toString(query.value(2)));
	ui->birth_date->setText(toString(query.value(3)));
    return true;
}

Generated Class Methods

clear()

This method clears the form widgets to make of the fields empty.

toVarinat(QString string)

This method converts empty QString to NULL value to store it in the database column as null. If the Qstring is empty it will return a NULL QVariant else it will return a QVariant of the QString value.
This method is used in insert and update.

toString(QVariant value)

This method is used to convert the null values stored in the database field into empty Qstring.
In some versions of Qt casting QVariant of null value to string returns a none empty string the returned value will be 0 or 0.00 or any other value depending of the data type of the field.
Casting null Inegter to 0 may be useful sometimes but it may cause a problem as an example if the field is a foreign key column field.

insert()

This method contains the insert statement and binding query values to execute the query and insert values to the database table.

update()

This method contains the update statement and binding query values to execute the query and update values in the database table.

remove() Mehod

This method contains the delete statement and binding query values to execute the query and delete values from the database table.

query() Mehod

This method contains the query statement and binding query values to execute the query and retrieve values from the database table and set the widgets values with these values.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.