QT C++ Database Programming Video Tutorial
Rate this article
0 out of 5
Database programming is an indispensable part of any type of computer programming languages. Developing a Database application using QT C++ is also of prime importance as the number of QT C++ based cross platform applications are slowly increasing. In this video tutorial, we will show you how to develop a basic QT C++ database application using an Example StudentDatabase.




Main Source Code


Studentdb.h

#ifndef STUDENTDB_H
#define STUDENTDB_H

#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlTableModel>

namespace Ui {
    class StudentDB;
}

class StudentDB : public QMainWindow {
    Q_OBJECT
public:
    StudentDB(QWidget *parent = 0);
    ~StudentDB();
    QSqlTableModel *model;
        QSqlDatabase db;
         bool createDB(QString dbname);
         void setName(const QString n);
         void setRollNO(const QString p);
         void AddNewStudent(QString Name,QString Roll_No);
         void ViewDetails();


protected:
    void changeEvent(QEvent *e);

private:
    Ui::StudentDB *ui;

private slots:
    void on_pushButtonView_clicked();
    void on_pushButtonDelete_clicked();
    void on_pushButtonAdd_clicked();
};

#endif // STUDENTDB_H

Studentdb.cpp

#include "studentdb.h"
#include "ui_studentdb.h"
#include <QDir>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>


StudentDB::StudentDB(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::StudentDB)
{
    ui->setupUi(this);
    QString path ;
        path="/home/rpi/Desktop/StudentDatabase";
        QDir file;
        //QString filepath=QDir::path();
        QString filename = file.path() + QDir::separator() + "student.db";

        if(!createDB(filename))
        {
            QMessageBox::critical(this,
                                  tr("Database not found"),
                                  tr("Database not found. The application will be closed."),
                                  QMessageBox::Ok);
            qApp->exit();
        }

}

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

void StudentDB::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
bool StudentDB::createDB(QString dbname)
{
    db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(dbname);

        if(db.open())
        {

            bool found = false;
            foreach (QString table, db.tables())
            {
                if(table == "student")
                {
                    found = true;
                    break;
                }
            }
            if(!found)
            {
                QSqlQuery query(db);
                query.exec("CREATE TABLE student (name VARCHAR(32), Roll_No VARCHAR(16))");
            }

            model = new QSqlTableModel(this,db);
            model->setTable("student");

            model->setEditStrategy(QSqlTableModel::OnFieldChange);
            model->select();


        }
        else
            return false;

        return true;



}

void StudentDB::on_pushButtonAdd_clicked()
{
    QString Name=ui->textName->toPlainText();
        QString RollNo=ui->textRollNo->toPlainText();

        AddNewStudent(Name,RollNo);
}
void StudentDB::AddNewStudent(QString Name, QString Roll_No)
{
    QSqlRecord rec = model->record();
        rec.setValue("name",Name);
        rec.setValue("Roll_No",Roll_No);


        // insert a new record (-1)
        model->insertRecord(-1,rec);
        ui->textName->setText("");
        ui->textRollNo->setText("");

}

void StudentDB::on_pushButtonDelete_clicked()
{
    QModelIndex sample = ui->treeView->currentIndex();
        if( sample.row() >= 0 )
        {
            QMessageBox::StandardButton dlg;
            dlg = QMessageBox::question(this, tr("Remove Student"),
                                        QString(tr("Remove Student ?")),
                                        QMessageBox::Yes | QMessageBox::No);

            if(dlg == QMessageBox::Yes)
            {
                // remove the current index
                model->removeRow(sample.row());
            }
        }
}

void StudentDB::on_pushButtonView_clicked()
{
    QTreeView  *view;
        view=ui->treeView;


         ViewDetails();

}
void StudentDB::ViewDetails()
{
    QTreeView *view=ui->treeView;
       view->setModel(model);

}

main.cpp

#include <QtGui/QApplication>
#include "studentdb.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    StudentDB w;
    w.show();
    return a.exec();
}




Download StudentDatabase Source Code

About The Author
KTS Training

KTS InfoTech Training division offers various short-term programming courses for Students and Professionals who are looking for a bright career in the field of Software development.

The programming video tutorials presented in this web site are taken from some of the training sessions conducted by KTS InfoTech.

You can also learn the Programming packages through Internet directly form our English Speaking Technical experts at an affordable rate.

For more details click here and submit your training requirements.



Number Of articles Posted      : 60
Number Of Messages Posted  : 3
Joined Date : 2012/11/14
   
Author
Messages
Posted:
Post Your Comments  
Name (Max 50 Chars)
Comments
 
 

 
TekTipsDownload
GateExam
Academic Projects
TekTipsExperts
Android