QT C++ Database Programming Video
Posted Date Unknown 0 Comment

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();
}



See All Responses Below...
Author
Message Rating
Posted on:
Please Login to Post Your Comments
Name (Max. 100 characters)
Please post your comments here
Select Language
Comments
   Design  HTML
Attach File(Max. Size: 2 MB)
A few simple rules when posting your Comments,
  1. Please post only answers relevant to the topic of discussion.
  2. Please dont misuse this site or do not be abusive, offensive, inappropriate,harass anyone on the boards or post ads or spam. Doing so will delete your inappropriate messages and will block or delete your account on this site. 

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud