Вы находитесь на странице: 1из 12

6

: . . -91
..
:
..

, 2013

6

:
.
: Qt,
,
, .
-,
-.


stud ()
*
cod_stud
I
disc()
*
cod_disc
I
sem ()
*
cod_sem
I
prepod()
*
cod_prep
I


FIO_stud
A(100)

n_disc
A(100)

n_sem
A(50)

FIO_prep
A(100)

cod_sem* cod_stud* cod_disc*


I

kol_la
b
I

kol_lekc kol_pr ekz za


c
I
I
I
I


(table_1->cod_prep):(prepod->cod_prep)->(0;):(1;1)
(table_1->cod_disc):(disc->cod_disc)->(0;):(1;1)
(table_2->cod_sem):(sem->cod_sem)->(0;):(1;1)
(table_2->cod_stud):(stud->cod_stud)->(0;):(1;1)
(table_2->cod_disc):(disc->cod_disc)->(0;):(1;1)

:

(, ):

kr
I

..

*
cod_disc
I

table_1 (-)

cod_prep
I
table_2

kp kontr
I


CREATE TABLE stud(
cod_stud integer PRIMARY KEY,
FIO_stud varchar(100) UNIQUE NOT NULL);
CREATE TABLE disc(
cod_disc integer PRIMARY KEY,
n_disc varchar(100) UNIQUE NOT NULL);
CREATE TABLE sem(
cod_sem integer PRIMARY KEY,
n_sem varchar(50) UNIQUE NOT NULL);
CREATE TABLE prepod(
cod_prep integer PRIMARY KEY,
FIO_prep varchar(100) UNIQUE NOT NULL);
CREATE TABLE table_1(
cod_prep integer REFERENCES prepod(cod_prep) ON UPDATE CASCADE ON DELETE
CASCADE,
cod_disc integer PRIMARY KEY REFERENCES disc(cod_disc) ON UPDATE CASCADE ON
DELETE CASCADE);
CREATE TABLE table_2(
cod_sem integer REFERENCES sem(cod_sem) ON UPDATE CASCADE ON DELETE CASCADE,
cod_stud integer REFERENCES stud(cod_stud) ON UPDATE CASCADE ON DELETE CASCADE,
cod_disc integer REFERENCES table_1(cod_disc) ON UPDATE CASCADE ON DELETE
CASCADE,
kol_lab integer,
kol_lekc integer,
kol_pr integer,
ekz integer,
zac integer,
kr integer,
kp integer,
kontr integer,
PRIMARY KEY(cod_sem, cod_stud, cod_disc));
CREATE SEQUENCE serial START 1;

, ,
-
void PrepodDictionary::updateTable()
{
delete Model;
Model = new QSqlQueryModel();
Model->setQuery("SELECT * FROM Prepod ORDER BY fio_prep");
ui->Table->setModel(Model);
ui->Table->setColumnHidden(0, true);
Model->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit(""));
connect(ui->Table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
this, SLOT(OnSelectionChanged()));
}

-,
.
:
void PrepodDictionary::OnSelectionChanged()
{
currentIndex = ui->Table->currentIndex().row();
updateAssignedDiscTable();
}

void PrepodDictionary::updateAssignedDiscTable()
{
delete assignedDiscsModel;
assignedDiscsModel = new QsqlQueryModel();
assignedDiscsModel->setQuery("SELECT n_disc, cod_disc FROM disc WHERE cod_disc IN
(SELECT cod_disc FROM table_1 WHERE cod_prep = "+Model>record(currentIndex).value(0).toString()+")");
ui->discTable->setModel(assignedDiscsModel);
ui->discTable->setColumnHidden(1, true);
assignedDiscsModel->setHeaderData(0, Qt::Horizontal, QString::fromLocal8Bit(""));
}

,
#include "StudDictionary.h"
#include "ui_StudDictionary.h"
#include <QtSql>
#include <QDebug>
#include <QMessageBox>
StudDictionary::StudDictionary(QWidget *parent) :
QWidget(parent),
ui(new Ui::StudDictionary)
{
studModel = NULL;
ui->setupUi(this);
setEditMode(false);
updateStudTable();
currentIndex = 0;
}
StudDictionary::~StudDictionary()
{
delete studModel;
delete ui;
}
void StudDictionary::setEditMode(bool isEdit)
{
ui->studSaveButton->setVisible(isEdit);
ui->studCancelButton->setVisible(isEdit);
ui->studAddButton->setVisible(!isEdit);
ui->studEditButton->setVisible(!isEdit);
ui->studDeleteButton->setVisible(!isEdit);
ui->studTable->setEnabled(!isEdit);
ui->studFilterLine->setEnabled(!isEdit);
ui->label_2->setEnabled(!isEdit);
}
void StudDictionary::on_studEditButton_clicked()
{
ui->studLine->setText(ui->studTable->currentIndex().data().toString());
ui->studLine->setFocus();
ui->studLine->selectAll();
setEditMode(true);
}
void StudDictionary::on_studCancelButton_clicked()
{
setEditMode(false);

ui->studLine->clear();
ui->studTable->setFocus();
ui->studTable->selectRow(currentIndex);
}
void StudDictionary::on_studAddButton_clicked()
{
if(ui->studLine->text().isEmpty())
return;
QSqlQuery q;
q.prepare("INSERT INTO stud VALUES(nextval('serial'), ?)");
q.bindValue(0, ui->studLine->text());
q.exec();
updateStudTable();
}
void StudDictionary::updateStudTable()
{
delete studModel;
studModel = new QSqlQueryModel();
studModel->setQuery("SELECT * FROM Stud ORDER BY FIO_Stud");
ui->studTable->setModel(studModel);
ui->studTable->setColumnHidden(0, true);
studModel->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit(""));
connect(ui->studTable->selectionModel(),
SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(OnSelectionChanged()));
}
void StudDictionary::OnSelectionChanged()
{
currentIndex = ui->studTable->currentIndex().row();
}
void StudDictionary::on_studSaveButton_clicked()
{
QSqlQuery q;
q.prepare("UPDATE stud SET FIO_Stud = ? WHERE cod_stud = ?");
q.bindValue(0, ui->studLine->text());
q.bindValue(1, studModel->record(currentIndex).value(0));
q.exec();
updateStudTable();
on_studCancelButton_clicked();
emit updateStudDict();
}

void StudDictionary::on_studDeleteButton_clicked()
{
if(QMessageBox::warning(this, QString::fromLocal8Bit(""),
QString::fromLocal8Bit("
?"),QMessageBox::Yes, QMessageBox::No)==QMessageBox::No)

return;
QModelIndexList indexes = ui->studTable->selectionModel()->selectedIndexes();
foreach(QModelIndex index, indexes)
{
QSqlQuery q;
q.prepare("DELETE FROM stud WHERE cod_stud = ?");
q.bindValue(0, studModel->record(index.row()).value(0));
q.exec();
}
updateStudTable();
emit updateStudDict();
}
void StudDictionary::on_studFilterLine_textChanged(const QString &arg1)
{
if(ui->studFilterLine->text().isEmpty())
{
updateStudTable();
}
else
{
delete studModel;
studModel = new QSqlQueryModel();
studModel->setQuery("SELECT * FROM Stud WHERE FIO_Stud LIKE '%"+ui->studFilterLine>text()+"%' ORDER BY FIO_Stud");
ui->studTable->setModel(studModel);
ui->studTable->setColumnHidden(0, true);
studModel->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit(""));
connect(ui->studTable->selectionModel(),
SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(OnSelectionChanged()));
}
}

,
.
void MainWindow::on_establishConnectionAction_triggered()
{
db = QSqlDatabase::addDatabase("QPSQL");
db.setDatabaseName(conSettings.DatabaseName);
db.setHostName(conSettings.DatabaseIP);
db.setUserName(conSettings.UserName);
db.setPassword(conSettings.UserPassword);
if(!db.open())
{
QMessageBox::critical(this, QString::fromLocal8Bit(""), QString::fromLocal8Bit("
. ."));
return;
}

setInterfaceMode(true);
updateAll(true);
timer.start(1000);
}
void MainWindow::on_disconnectAction_triggered()
{
timer.stop();
db.close();
QSqlDatabase::removeDatabase(db.connectionName());
setInterfaceMode(false);
}
void MainWindow::STimer()
{
QSqlQuery query(db);
if(!query.exec("SELECT 1"))
on_disconnectAction_triggered();
}

:
. ,
Qt Qt Creator.